Updating chart data

In place update

If the labels on the X-Axis are not changing, then you can perform an "in-place-update" of chart data. This has the advantage of smoothly scaling the chart so that your users clearly understand how much it changes.

You can update a chart's data anytime after it's rendered using the updateSeries function. Remember to use the correct series ID, the same one you used while creating the series with addSeries For example:

  public function buildDashboard () { 
    // ... Configure two charts $chart1 and $chart2
    $chart->onItemClick (array($chart2), "handleItemClick");
  }

  public function handleItemClick ($source, $targets, $params) {
    $chart2 = $this->getComponentByID("chart2");
    $chart2->updateSeries("series_1", [3, 5, 2]);
  }

Complete Example

<?php

class SampleDashboard extends StandaloneDashboard { public function buildDashboard(){ $chart = new ChartComponent("chart1"); $chart->setCaption("My First Chart"); $chart->setDimensions (4, 4); $chart->setLabels (["Jan", "Feb", "Mar"]); $chart->addSeries ("beverages", "Beverages", array(1355, 1916, 1150)); $chart->addSeries ("packaged_foods", "Packaged Foods", array(1513, 976, 1321));

$this->addComponent ($chart);

$chart2 = new ChartComponent("chart2");
$chart2->setDimensions (4, 4);
$chart2->setCaption ("My First Chart");
$chart2->setLabels (array("A", "B", "C"));
$chart2->addSeries ("series_1", "Series 1", array(1, 2, 3));
$this->addComponent ($chart2);

$chart->onItemClick (array($chart2), "handleItemClick", $this);

}

public function handleItemClick ($source, $targets, $params) { $chart2 = $this->getComponentByID("chart2"); $chart2->updateSeries("series_1", [3, 5, 2]); }

}

$db = new SampleDashboard(); $db->renderStandalone();

Full chart update

If you are re-drawing new data on the chart, you will need to first clear out the contents of the chart. To do so, use the clearChart function. Using this function it is almost like creating a new chart altogether, because this clears out all the series. Note that this doesn't reset other properties like the Y Axis name, etc.

  public function buildDashboard () { 
    // ... Configure two charts $chart1 and $chart2
    $chart->onItemClick (array($chart2), "handleItemClick");
  }

  public function handleItemClick ($source, $targets, $params) {
    $chart2 = $this->getComponentByID("chart2");
    $chart2->clearChart ();
    $chart2->setLabels (array ("E", "F", "G", "H", "I"));
    $chart2->addSeries ("series_3", "Series 3", [3, 5, 2, 1, 6]);
  }

Complete Example

<?php

class SampleDashboard extends StandaloneDashboard { public function buildDashboard(){ $chart = new ChartComponent("chart1"); $chart->setCaption("The first Chart"); $chart->setDimensions (4, 4); $chart->setLabels (["Jan", "Feb", "Mar"]); $chart->addSeries ("beverages", "Beverages", array(1355, 1916, 1150)); $chart->addSeries ("packaged_foods", "Packaged Foods", array(1513, 976, 1321));

$this->addComponent ($chart);

$chart2 = new ChartComponent("chart2");
$chart2->setDimensions (4, 4);
$chart2->setCaption ("Second chart");
$chart2->setLabels (array("A", "B", "C"));
$chart2->addSeries ("series_1", "Series 1", array(1, 2, 3));
$this->addComponent ($chart2);

$chart->onItemClick (array($chart2), "handleItemClick", $this);

}

public function handleItemClick ($source, $targets, $params) { $chart2 = $this->getComponentByID("chart2"); $chart2->clearChart (); $chart2->setLabels (array ("E", "F", "G", "H", "I")); $chart2->addSeries ("series_3", "Series 3", [3, 5, 2, 1, 6]); }

}

$db = new SampleDashboard(); $db->renderStandalone();