Menu

Custominzing text-data Ticks

gw1500se
2015-08-20
2015-08-22
  • gw1500se

    gw1500se - 2015-08-20

    I have a text-data plot and want to customize the ticks/labels on the X-axis. I was not able to find much on handling text ticks in the documentation. I want to have the ticks distributed evenly along the X-axis (the number of text items will be increasing), starting a X=0, based on the number of X items. XTickAnchor does not seem to apply to text data but I think I want the equivalent of setting it to 0. TIA.

     
  • lbayuk

    lbayuk - 2015-08-21

    To use tick marks with 'text-data' data type, and have a mark below each data point, just include these:

    $plot->SetXTickAnchor(0.5);
    $plot->SetXTickIncrement(1);
    

    SetXTickAnchor is necessary due to how PHPlot assigns X values to your array when you don't specify them. Setting the X Tick Increment to 1 is not actually needed until you get to about 16 data points (where PHPlot would default to an increment of 2).

    Using 'text-data' data type doesn't really fit with use of X axis tick labels. With 'text-data', you are telling PHPlot you don't care about the X values of your data - you just want them plotted at uniform intervals. X axis tick labels are meant to mark specific values on the X axis, which aren't meaningful with 'text-data'. You can, of course, use X axis data labels (the first value in each row of your data array) here.

     
  • gw1500se

    gw1500se - 2015-08-21

    Thanks for the reply. I'm not sure I understand all this but I'll give it a try. You are correct that I don't care what the X values are and that they should be at uniform intervals. It is where things are located that matter. For example suppose I have 3 data points. The 1st should be at X=0, the 2nd should be centered and the 3rd should be at the right end of the X-axis. In other words the end point should be at the extremes of the X- axis and the rest equally distributed between.

     
  • gw1500se

    gw1500se - 2015-08-21

    Your suggestion didn't work and actually did not change anything. I have 2 points at the moment. It is placing the first 1/3 along the X-Axis and the second 2/3 along the axis. What I need, as I said, with 2 points there should be one at each end of the axis.

     
  • lbayuk

    lbayuk - 2015-08-21

    That's a different thing entirely. The 2 functions I used above change the X values where tick marks will be drawn. They have nothing to do with where the X values are positioned along the X axis line.

    For that, you want to set the X range (the mapping between World Coordinates to Device Coordinates) with SetPlotAreaWorld(). The first argument is Xmin, which should be 0.5 for the first point when using text-data. The third argument is Xmax, which should be N-0.5 for N points. For example, if you have 3 data points, this will put the first and last on the left and right edges respectively, and the second in the middle:

    $plot->SetPlotAreaWorld(0.5, NULL, 2.5);
    
     
  • gw1500se

    gw1500se - 2015-08-21

    Thanks again and I am getting closer but not quite there. That moved the first point to X=0 but the second point is now 1/3 along the axis.

    $plot->SetPlotAreaWorld(.5,null,count($finishes)-.5);

    count($finishes) returns 2.

     
  • lbayuk

    lbayuk - 2015-08-22

    Works for me. Here is my test script. Set n=2 to get 2 data points per line. The points and tick marks are on the left and right edges of the plot. (Are you sure your count()==2?)

    <?php
    # Forum 2015-08-19
    # Tick marks with text-data, vs number of points
    require_once 'phplot.php';
    
    $n = 2; // Number of X data points: change as wanted
    
    $data = array();
    for ($i = 0; $i < $n; $i++) {
        $data[] = array("=$i=", $i, 2 * $i, 3 * $i, 4 * $i);
    }
    
    $plot = new PHPlot(800, 800);
    $plot->SetLineStyles('solid');
    $plot->SetDataType('text-data');
    $plot->SetDataValues($data);
    $plot->SetPlotType('linepoints');
    $plot->SetXTickAnchor(0.5);
    $plot->SetXTickIncrement(1);
    $plot->SetPlotAreaWorld(0.5, NULL, count($data)-0.5);
    $plot->DrawGraph();
    
     
  • gw1500se

    gw1500se - 2015-08-22

    Yes, I'm sure. There must be some other parameter I am using that is countering the setting. Here is my entire code:

    $plot=new PHPlot(900,700);
    $plot->SetTTFPath("/usr/share/fonts/msttcore");
    $plot->SetDefaultTTFont("times.ttf");
    $plot->SetUseTTF(true);
    $plot->SetPrintImage(false);
    $plot->SetPlotType('lines');
    $plot->SetDataType('text-data');
    $plot->SetYTickLabelPos('none');
    $plot->SetYTitle('Score/Finish');
    $plot->SetXTitle('Date');
    $plot->SetXTickLabelPos('none');
    $plot->SetDrawYGrid(false);
    $plot->SetDrawXGrid(false);
    $plot->SetPlotAreaWorld(.5,null,count($finishes)-.5);
    $plot->SetLineWidths(3);
    $plot->SetLegendUseShapes(true);
    $plot->SetLegend(array("Overall Score","Chicken Score","Pork Ribs Score","Pork Score","Brisket Score","Overall Place","Chicken Place","Pork Ribs Place","Pork Place","Brisket Place"));
            $plot->SetLineStyles(array('solid','solid','solid','solid','solid','dashed','dashed','dashed','dashed','dashed'));
            $plot->SetDataColors(array('black','red','green','blue','brown','black','red','green','blue','brown'));
    $plotarray=findFinishes($finishes,$categories,$data);   
    $plot->SetNumXTicks(count($plotarray));
    $plot->SetDataValues(null);
    $plot->SetDataValues($plotarray);
    $plot->SetTitle("Trends");
    $plot->DrawGraph();
    echo "<div align=\"center\">";
    echo "<img src=\"".$plot->EncodeImage()."\" />";
    

    P.S. What is the encoding for code? All I see is numbered lists and quote.

     

    Last edit: lbayuk 2015-08-22
  • lbayuk

    lbayuk - 2015-08-22

    Hope you don't mind, but I put ~~~~ (4 tildes) around your code, to keep Markdown from eating parts of it. In answer to your P.S.: blank line and ~~~~ before the code, and ~~~~ after the code, is one way. Markdown will then leave it alone. (But it won't colorize it unless it starts with a PHP begin tag.)

    Two things I noticed in your code. First, you are using $plotarray as your data array, but using a different array $finishes in SetPlotAreaWorld(). I can't tell if they are the same size, but I would definitely use count($plotarray) in SetPlotAreaWorld() just to make it clear.

    The second thing is SetNumXTicks(). I would take it out, perhaps using the functions we talked about before - SetXTickIncrement and SetXTickAnchor. The problem is that using SetNumXTicks actually sets the number of intervals, not the number of ticks. Also using it overrides some of the other tick calculations.

     
  • gw1500se

    gw1500se - 2015-08-22

    Thanks. SetNumTicks was it. That should not have been there and it took new eyes to see it.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.