Menu

"Web Form with Plot" -- zero...

bvn1
2012-06-13
2012-09-07
  • bvn1

    bvn1 - 2012-06-13

    I've been working with the "Complete Web Form with Plot" code found in the
    documentation (http://www.phplot.com/phplotdocs/ex-
    webform.html),
    but
    when I try to input values that are equal to zero or negative no graph is
    drawn. No error appears either -- the url indicates the values were sent, but
    no graph actually appears on the page.

    I'm a huge beginner and my code right now is essentially right from the
    reference example -- I just can't figure out how to get the graph to work when
    the user inputs values that are zero or negative -- am I missing something?

    Thanks for any help!

     
  • lbayuk

    lbayuk - 2012-06-13

    Hello.

    The web form example takes a rather hard-line approach to parameter validity.
    If the web parameters are not in the expected form, it considers the form
    submission invalid and simply does not produce a graph. It just redisplays the
    form with the default values. The function check_form_params() does this. (For
    security, web forms should be very strict about parameter checking, right?)
    Yes, an error message would be appropriate for a real application, but this is
    an example, and I try to keep them short.

    Now I confess I expected it to reject values that are < 0, but not 0 itself. I
    see it does reject 0 also (because I used empty() and often forget that
    empty(0) is true). A small bug. Nevertheless, it is showing you compounded
    interest on a savings account with fixed monthly deposits, and it doesn't
    really make sense to have a deposit <= 0 or interest rate <= 0.

    Assuming it works for you with values > 0, it is working as designed, and you
    can change it to do whatever you want. Feel free to post any other questions
    here, and I'll try to help.

    (By the way, I did not realize that www.phplot.com was working - thanks for
    that! I asked the domain owner if he wanted to redirect it to the Sourceforge
    virtual hosting several months ago, but I never heard back from him, and it
    wasn't set up last time I checked.)

     
  • bvn1

    bvn1 - 2012-07-03

    Thanks for the reply (I realize I am replying very late here...)!

    The reason I wanted to use negative values is because I'm trying to create a
    graph where users can input certain values that will affect the output of the
    graph (and create a kind of mini-portfolio graph) -- and in this case negative
    values could also be a possibility.

    I do have a second question, though. I am modifying the graph to be a scatter
    plot, and I am wondering if there's a good way to add a label to each of the
    data points that says what it is. For example, if I graphed a point at
    (50,50), is there a good way to add a small label on the top of that point
    that says "Point 1," and then for a second point at say (75, 75), have text
    above that point that says "Point 2" and so on? Or at least change the color
    of each point on a single graph manually (rather than have color change be
    based on point value)?

    I was considering possibly overlaying multiple graphs with one point on each
    to create the effect I'm looking for -- is that the best way to go about this?

    Thanks again for the prompt response before -- I apologize I am not nearly as
    on time.

     
  • lbayuk

    lbayuk - 2012-07-03

    Thanks for following up. Don't make the same mistake I made when checking form
    parameters (using empty() to check for missing parameter, when I should be
    using !isset() so it allows for 0). I did fix it in the manual, but it won't
    appear until the next release.

    Scatter plots ("points" plot to PHPlot) do support Data Value Labels, which
    are labels near each data point. You need PHPlot-5.3.0 or later for this.
    There is an example in the manual, although this uses a linepoints plot with both lines
    and points, and also has data label lines (vertical lines down to the axis).
    By default the labels are slightly above each point, but you can change this
    if you want.

    By default, these labels show the Y value of each point. But you can use a
    custom label formatting function to make them show anything you want. You set
    this up with $plot->SetYDataLabelType('custom', 'myfunction', ...).

    There is also a way to control the color of each point (custom data color
    selection callback).

    Let me know if you want, and I can put together a more specific example.

    I don't recommend overlaying plots, unless the above really can't do what you
    want.

     
  • bvn1

    bvn1 - 2012-07-18

    Thank you for all your help so far! Zero values do indeed work now that
    !isset() is being used -- thanks again. However, I do admit I was still having
    a little troubling getting custom labels and colors to work correctly despite
    your advice and detailed examples, so here is where I am so far:

    Essentially, I'd like the names of each point ("portfolio", "tpx", "spx") to
    be the label on the top of each point rather than the Y values, possibly with
    a different color for the "portfolio" point as well. Here's what some of my
    code looks like so far, if that helps:

    $data = array(
        array('', 0, 0), //initial point
            array('tpx', $standard_deviation_tpx, $tpx_return),
            array('spx', $standard_deviation_spx, $spx_return),
        array('portfolio', $standard_deviation_tpx * $variable1, $tpx_return * $variable2),
            );
    
    function draw_graph($valid_params, $param, $data)
    {
        extract($param);
    
        $plot = new PHPlot($w, $h);
        $plot->SetTitle('Calculated Portfolio Risk/Return');
        $plot->SetDataType('data-data');
        # Don't set data values if parameters were not valid. This will result
        # in PHPlot making an image with an error message.
        if ($valid_params) {
            $plot->SetDataValues($data);
        }
        $plot->SetLegend(array('Risk/Return',));
        $plot->SetLegendPixels(400, 50);
        $plot->SetXTitle('Risk');
        $plot->SetXTickIncrement(50);
        $plot->SetYTitle('Return');
        $plot->SetYLabelType('data', 2);
        $plot->SetDrawXGrid(False);
        $plot->SetDrawYGrid(False);
        $plot->SetPlotType('points');
        $plot->SetYDataLabelPos('plotin');
    
     
  • lbayuk

    lbayuk - 2012-07-18

    Your first part (label value) is easy, but the second part (label color) is
    unfortunately not directly supported in PHPlot.

    To have the 'name' of each point displayed above the value, use a custom label
    formatting function that pulls the label value out of your data array. Here is
    is an example of the function:

    // Custom Y data label format function:
    function show_label($value, $data, $row, $column)
    {
        return $data[$row][0];    //  Return the row label from the data array
    }
    

    Now you tell PHPlot to call that function to format each data value label:

    $plot->SetYDataLabelType('custom', 'show_label', $data);
    

    Note I am passing your data array $data using the 'callback_arg' argument, so
    the function can access the array without declaring it global, but it is also
    OK to skip this argument and just declare global $data.

    Although there is a way to customize the color of each data point, the labels
    will always be the same color (SetDataLabelColor() or SetTextColor()).

     
  • bvn1

    bvn1 - 2012-07-19

    You are awesome! Thanks so much for all the help considering I'm ridiculously
    clueless with this kind of stuff. The code works like a charm.

    And sorry if I wasn't clear earlier -- it's also perfectly fine if for the
    point to be a different color; no need to have a different label color. I will
    play around with the point color settings.

    There's just one (and hopefully last) issue that you may be able to answer,
    which is how to get the x-axis values to appear back on the graph. Here's
    where I'm at:

    Basically, I just need the x-values to appear on the x-axis the same way the
    y-values currently appear on the y-axis (because I do not want the point label
    names to be on the x-axis the way it is now). Is there an easy way to toggle
    the x-values on the x-axis back on?

     
  • lbayuk

    lbayuk - 2012-07-19

    You want to see X Tick Labels instead of X Axis Data Labels (to use the same
    terms as the reference manual, I think). SInce your data array does have
    labels (rather than empty strings), PHPlot assumes you want to see them on the
    axis instead of the tick values. You do need the labels in the array, for your
    label function, but you want to see the tick values, so just tell PHPlot you
    want tick labels along the bottom axis:

    $plot->SetXTickLabelPos('plotdown')
    

    Or tell it you don't want data labels on the axis:

    $plot->SetXDataLabelPos('none')
    

    Either (or both) will result in tick labels instead of data labels along the
    axis.

     

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.