Ask Your Question
3

How to create multiple plots in jsGraph (PHP) using arrays of varying sizes?

asked 2021-10-02 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-02-16 01:00:00 +0000

nofretete gravatar image

To create multiple plots in jsGraph using arrays of varying sizes, you will need to use loops and conditional statements. Here is a sample code that demonstrates how to achieve this:

<?php 
  // Sample data for plotting 
  $xdata = array(0, 1, 2, 3, 4); 
  $ydata1 = array(0, 2, 4, 6, 8); 
  $ydata2 = array(1, 3, 5); 
  $ydata3 = array(1, 2, 3, 4); 

  // Initialize jsGraph object 
  require_once('jsgraph/jsgraph.php'); 
  $graph = new Graph(); 

  // Create data sets 
  $datasets = array( 
    array($xdata, $ydata1), 
    array($xdata, $ydata2), 
    array($xdata, $ydata3) ); 

  foreach ($datasets as $idx => $data) { // loop through each dataset 
    $xdata = $data[0]; 
    $ydata = $data[1]; 
    $dataset = new DataSet(); 
    $dataset->AddPoints($xdata, $ydata); 

    // Create new plot 
    $plotNew = new LinePlot($dataset); 
    $plotNew->SetColor('blue'); 
    $plotNew->SetWeight(2); 

    // Add plot to graph 
    $graph->Add($plotNew); 
  } 

  // Generate graph 
  $graph->Stroke(); 
?>

In this code, we first define the sample data for plotting, including three arrays of varying sizes. We then initialize the jsGraph object and create an array of data sets, with each data set containing an array of x-data and y-data.

Next, we loop through each data set and extract the x-data and y-data arrays. We then create a new DataSet object and populate it with the x-data and y-data arrays using the AddPoints() method.

We then create a new LinePlot object and set its color and weight properties. We add the plot to the graph using the Add() method.

Finally, we generate the graph using the Stroke() method.

Note that in this example, we assume that the x-data array is the same for all plots. If your x-data arrays are different, you will need to modify the code accordingly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-02 11:00:00 +0000

Seen: 7 times

Last updated: Feb 16 '23