$data1, 'series2_label' => $data2, ). * @param string $title Chart title. * * @return string Chart.js JS file & Chart JS or empty string if error */ function ChartjsChart( $type, $data, $title ) { static $chart_id = 0; $types = [ 'line', 'bar', 'doughnut', 'pie' ]; if ( ! in_array( $type, $types ) || ! is_array( $data ) || empty( $data ) ) { return ''; } // @link https://github.com/chartjs/Chart.js/issues/815#issuecomment-270186793 // @link http://clrs.cc/ $colors_default = [ '#FF851B', '#2ECC40', '#0074D9', '#F012BE', '#FFDC00', '#3D9970', '#001f3f', '#85144b', '#FF4136', '#01FF70', '#39CCCC', '#B10DC9', '#DD6300', '#50EE62', '#2296FB', '#CE009C', '#DDBA00', '#5FBB92', '#224161', '#A7366D', '#DD1914', '#23FF91', '#61EEEE', '#8F00A7' ]; $dataset_default = [ 'label' => '', 'backgroundColor' => $colors_default, // 'borderColor' => 'rgb(255, 99, 132)', 'data' => [], ]; $first_key = key( $data ); if ( $type === 'bar' && is_string( $first_key ) ) { // Stack series. $i = 0; foreach ( (array) $data as $label => $data_serie ) { $labels = array_values( $data_serie[0] ); // Force start index to 0. $dataset = $dataset_default; $dataset['label'] = $label; $dataset['backgroundColor'] = $colors_default[ $i % count( $colors_default ) ]; $dataset['data'] = array_values( (array) $data_serie[1] ); // Force start index to 0. $datasets[ $i ] = $dataset; $i++; } } else { $labels = array_values( $data[0] ); // Force start index to 0. $dataset = $dataset_default; $dataset['data'] = array_values( (array) $data[1] ); // Force start index to 0. $datasets = [ $dataset ]; } // Chart Options. $chart_options = ''; if ( $type === 'line' || $type === 'bar' ) { // Line & Bar Chart Options. $chart_options .= 'scales: { x: { grid: { display: false // Turn off only the vertical gridlines. }, stacked: true }, y: { beginAtZero: true, stacked: true } },'; if ( ! is_string( $first_key ) ) { // Remove legend, empty anyway. $chart_options .= 'legend: { display: false, },'; } } ob_start(); if ( ! $chart_id ) : ?>