Menu

overlaying graphs.

2015-03-21
2015-03-25
  • PrakashGaonkar

    PrakashGaonkar - 2015-03-21

    http://www.phplot.com/phplotdocs/ex-outbreak.html
    I am using this example.

    My data is dynamic fetched from a csv file. The Y axis range can vary a lot.
    I need to overlay 2 charts such as they are independent. But in my code data1 plots fine.
    Data2 uses the Y axis (Right side) range same as Data1.
    I want data2 Y axis to use it's own data range. whatever phplot automatically calculates.

    code is as below.

        require_once 'phplot.php';
    
        $title = 'GDVM Rank vs Close';
        $y_title1 = 'Weekly Rank';
                # data1 $rank_ data is dynamic
        $data1 = array(
        array('12_13_2014',$Rank_12_13_2014),
        array('12_20_2014',$Rank_12_20_2014),
        array('12_27_2014',$Rank_12_27_2014),
        array('01_03_2015',$Rank_01_03_2015),
        array('01_10_2015',$Rank_01_10_2015),
        array('03_14_2015',$Rank_03_14_2015)        
                            );
        $legend1 = array('Weekly Rank');
    
        # Data for plot #2: linepoints:
        $y_title2 = 'Weekly Close'  ;
                # data2 $close_ data is dynamic             
        $data2 = array(
        array('12_13_2014',$Close_12_13_2014),
        array('12_20_2014',$Close_12_20_2014),
        array('12_27_2014',$Close_12_27_2014),
        array('01_03_2015',$Close_01_03_2015),
        array('01_10_2015',$Close_01_10_2015),
        array('03_14_2015',$Close_03_14_2015)       
                            );                  
        $legend2 = array('Weekly Close');
    
        $plot =& new PHPlot(1000,600);
        $plot->SetImageBorderType('plain'); // For presentation in the manual
        $plot->SetPrintImage(False);  // Do not output the image
        $plot->SetTitle($title);
        $plot->SetPlotBgColor('gray');
        $plot->SetLightGridColor('black'); // So grid stands out from background
    
            # Plot 1
        $plot->SetDrawPlotAreaBackground(True);
        $plot->SetPlotType('stackedbars');
        $plot->SetDataType('text-data');
        $plot->SetDataValues($data1);
        $plot->SetYTitle($y_title1);
        # Set and position legend #1:
        $plot->SetLegend($legend1);
        $plot->SetLegendPixels(5, 30);
        # Set margins to leave room for plot 2 Y title on the right.
        $plot->SetMarginsPixels(120, 120);
        # Specify Y range of these data sets:
        //$plot->SetPlotAreaWorld(NULL, 0, NULL, 5000);
        //$plot->SetYTickIncrement(500);
        $plot->SetXTickLabelPos('none');
        $plot->SetXTickPos('none');
        # Format Y tick labels as integers, with thousands separator:
        //$plot->SetYLabelType('data', 0);
        //$plot->TuneYAutoRange(10, 'Decimal');
        $plot->SetYLabelType('data', 1);
        $plot->DrawGraph();
    
        # Plot 2
        $plot->SetDrawPlotAreaBackground(False); // Cancel background
        $plot->SetDrawYGrid(False); // Cancel grid, already drawn
        $plot->SetPlotType('linepoints');
        $plot->SetDataValues($data2);
        # Set Y title for plot #2 and position it on the right side:
        $plot->SetYTitle($y_title2, 'plotright');
        # Set and position legend #2:
        $plot->SetLegend($legend2);
        $plot->SetLegendPixels(690, 30);
        # Specify Y range of this data set:
        //$plot->SetPlotAreaWorld(NULL, 0, NULL, 50);
        //$plot->SetYTickIncrement(10);
        //$plot->TuneYAutoRange(10, 'binary');
        $plot->SetYTickPos('plotright');
        $plot->SetYTickLabelPos('plotright');
        $plot->SetDataColors('black');
        # Format Y tick labels as integers with trailing percent sign:
        //$plot->SetYLabelType('data', 0, '', '%');
        $plot->SetYLabelType('data', 1);
        $plot->DrawGraph();
    
        //$graph->DrawGraph();
        echo "<img src=\"" . $plot->EncodeImage() . "\">\n";
    
     
  • PrakashGaonkar

    PrakashGaonkar - 2015-03-21

    Basically in these 2 situations the same code should automatiocally adjust both Y axis intervalsd.

    1: data1 changes in range 10 to 100
    data2 changes in range 40 to 50.
    2: data1 changes in range 1500 to 2000
    data2 changes in range 10 to 20.

    from my code, data1 (left Y axis) plots fine as barcharts.
    the data2 (right Y axis) tries to follow data1's y axis range and it's not preety.

     
  • PrakashGaonkar

    PrakashGaonkar - 2015-03-21

    copying from elsewhere on the forum..

    "PHPlot is supposed to automatically calculate a good range based on the data array for the first overlay, and then use the same range for the second - regardless of the second data array."

    Now That is the problem. I want the second data array to use it's own range. Is it possible in PHPlot?

     
  • PrakashGaonkar

    PrakashGaonkar - 2015-03-22

    trial and Error....
    I think below line solved my problem.(May be) Your comments are welcome.

    $plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);

     
  • lbayuk

    lbayuk - 2015-03-22

    Yes, you figured it out correctly. Each NULL argument to SetPlotAreaWorld tells PHPlot to calculate (or re-calculate, when used with overlays) a value for that limit. In your previous post, you are setting Ymin=0 and letting PHPlot recalculate Xmin, Xmax, and Ymax.

    More details here: http://www.phplot.com/phplotdocs/adv-multiplot.html#adv-multiplot-datascale

     
  • PrakashGaonkar

    PrakashGaonkar - 2015-03-22

    Cool. Thanks. I think it is a terrific project.

     
  • lbayuk

    lbayuk - 2015-03-22

    Thanks. By the way, I noticed your example above has: $plot &= new PHPlot(...). You should not use & with new in any current version of PHP. (If you took this from an old copy of the PHPlot Reference Manual on a web site somewhere, please let me know where.)

     
  • PrakashGaonkar

    PrakashGaonkar - 2015-03-25

    Thanks. I will change the code to remove "&". I don't remember from where i picked that code. if I see it again, I will update the message with link.

     

Log in to post a comment.