Assigns the last value set to every chart label

I created a field in a shortcode to set a footer label for highcarts. I got it to save the value set, it groups it with the chart id in the array and i managed to pass the string from the php file to the js function where it adds the footer label for the chart. But it always assign the last caption i set for a graph in a post to all the other graphs in that post. It probably is something very simple but im stuck in this part.
This is the php code:

        $fields = array(

        array(
            'label' => esc_html( 'Graph' ),
            'description' => esc_html( 'Choose the graph' ),
            'attr'    => 'chart',
            'type'    => 'select',
            'options' => $charts,
        ),
        array(
            'label' => esc_html( 'Footer label' ),
            'description' => esc_html( 'Choose the footer label' ),
            'attr' => 'footer_caption',
            'type' => 'text',
        ),

    );

public static function shortcode_ui_chart( $attr, $content, $shortcode_tag ) {

        $attr = shortcode_atts( array(
            'chart' => null,
            'footer_caption' => null,
        ), $attr, $shortcode_tag );

This is how i pass the value of the field to the js file:

<script>
  var captionLabel = "<?php echo $attr['footer_caption']; ?>";
</script>

And this is the js file where i add the footer label:

  chart: {
      type: 'column',
      events: {
          load: function () {
            var label = this.renderer.label(captionLabel)

This is what i get when i print_r $attr (just an example from 1 graph):

 [27-Apr-2017 11:03:27 UTC] Array
(
    [chart] => 23
    [footer_caption] => just another test...
)

The problem is that if i add a caption to the last graph of a post all the other graphs in that post get that caption and not their specific caption and i can see in the db that they are correctly assigned like this:

[the_chart chart="23" footer_caption="just another test..."/]

[the_chart chart="22" footer_caption="teste"/]

[the_chart chart="24" footer_caption="another test..."/]

So can someone tell me how i get it to assign the specific footer label to the right chart and not the last one that i set assigned to every other chart? I’m sorry if its not clear enough or a stupid question.

Read more here: Assigns the last value set to every chart label

Leave a Reply

Your email address will not be published. Required fields are marked *