ChartJs multiple datasets #1216
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
To create a ChartJS chart with multiple datasets in Backpack, similar to the one in the demo page, you can follow these steps:
Here's an example of how you can create a chart with multiple datasets: <?php
namespace App\Http\Controllers\Admin\Charts;
use Backpack\CRUD\app\Http\Controllers\ChartController;
use ConsoleTVs\Charts\Classes\Chartjs\Chart;
class NewEntriesChartController extends ChartController
{
public function setup()
{
$this->chart = new Chart();
// Set labels for the dataset points
$labels = [];
for ($days_backwards = 30; $days_backwards >= 0; $days_backwards--) {
$labels[] = $days_backwards.' days ago';
}
$this->chart->labels($labels);
// Set URL for AJAX data loading
$this->chart->load(backpack_url('charts/new-entries'));
// Optional configurations
$this->chart->minimalist(false);
$this->chart->displayLegend(true);
}
public function data()
{
// Simulate data for multiple datasets
$users = [/* ... */];
$articles = [/* ... */];
$categories = [/* ... */];
$tags = [/* ... */];
// Add multiple datasets
$this->chart->dataset('Users', 'line', $users)
->color('rgb(77, 189, 116)')
->backgroundColor('rgba(77, 189, 116, 0.4)');
$this->chart->dataset('Articles', 'line', $articles)
->color('rgb(96, 92, 168)')
->backgroundColor('rgba(96, 92, 168, 0.4)');
$this->chart->dataset('Categories', 'line', $categories)
->color('rgb(255, 193, 7)')
->backgroundColor('rgba(255, 193, 7, 0.4)');
$this->chart->dataset('Tags', 'line', $tags)
->color('rgba(70, 127, 208, 1)')
->backgroundColor('rgba(70, 127, 208, 0.4)');
}
} This example is based on the code provided in the Backpack documentation for Chart widgets. To use this chart in your Backpack admin panel, you can add it as a widget: [
'type' => 'chart',
'controller' => \App\Http\Controllers\Admin\Charts\NewEntriesChartController::class,
// Other widget configuration options...
] Unfortunately, I don't have information about a specific repository with the demo chart controller. However, the example provided above should give you a good starting point for creating a chart with multiple datasets. Remember to adjust the data generation in the Backpack Documentation - Chart Widget Relevant sources: Github Issue (closed): [Feature Request] multiple charts from one controller |
Beta Was this translation helpful? Give feedback.
-
Exactly, the easiest way would be to take a look at how that's done in our demo. See:
|
Beta Was this translation helpful? Give feedback.
Exactly, the easiest way would be to take a look at how that's done in our demo. See: