Skip to content Skip to sidebar Skip to footer

How To Parse And Pass Data To My Morris.js Chart

I want to use morris charts in my application to display some data. First, let me show you my js: var handleStoreIncomeShareDonutChart = function() { var green = '#00acac';

Solution 1:

You can convert it to the correct format with an array_map, and output it as a variable in a <script> before loading your handleStoreIncomeShareDonutChart file. The following code should work in PHP 5.3 and above.

$data = array(
  array(
    'name' => 'Store 1',
    'value' => 25,
  ),
  array(
    'name' => 'Store 2',
    'value' => 75,
  ),
);

$output = array_map(function ($record) {
  return array(
    'label' => $record['name'],
    'value' => $record['value'],
  );
}, $data);
echo '<script>var morris_data = ' . json_encode($output) . ';';

Post a Comment for "How To Parse And Pass Data To My Morris.js Chart"