• Hi, I have a type of custom post, called “valoraciones” in which I have custom fields added. I’m trying to use a custom field for score data in a gauge chart and I can’t figure out how to do it.

    This is the data that you would have to use as a “score”:
    $score_calidad_muscular = get_post_meta( get_the_ID(), 'calidad_muscular', true );

    And what occurred to me so far would be to do the following:
    [amcharts id="pie-1" data-score="$score_calidad_muscular"]

    Any help or an example to base myself on.
    Thank you very much in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author martynasma

    (@martynasma)

    I don’t think you can just insert a PHP variable into a shortcode like that.

    I suppose you may need to add a filter to populate shortcode data dynamically using your code.

    https://wordpress.org/plugins/amcharts-charts-and-maps/#are%20there%20any%20filters%20available%3F

    Thread Starter locofierro22

    (@locofierro22)

    Thanks for your reply.

    So I should use a filter to pass the value as data-score.

    Any example of how to apply it?
    I have very basic knowledge.

    From what I see I should use the following filter, but I don’t know how to adapt it

    amcharts_shortcode_data

    Plugin Author martynasma

    (@martynasma)

    Something like this:

    function apply_amcharts_shortcode_data( $output, $atts ) {
      // Load your data here
      // ...
    
      // Add to the output
      $output['score'] = ... your loaded data here ...
      return $output;
    }
    add_filter( 'amcharts_shortcode_data', 'apply_amcharts_shortcode_data' );

    In JS it will be accessible as AmCharts.wpChartData.score.

    Thread Starter locofierro22

    (@locofierro22)

    Something like this:

    Perfect. Let’s see if I’m on the right track …
    I have the following code to pass the data to the graph through a filter, but how do I associate that filter to a specific graph?

    
    function apply_amcharts_shortcode_data( $output, $atts, $post_id ) {
      // Load your data here
      $calidad_muscular = get_post_meta( $post_id, 'calidad_muscular', true );
    
      // Add to the output
      $output['score'] = $calidad_muscular
      return $output;
    }
    add_filter( 'amcharts_shortcode_data', 'apply_amcharts_shortcode_data' );
    
    Plugin Author martynasma

    (@martynasma)

    You can do in your chart code something like this:

    "dataProvider": AmCharts.wpChartData.score

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Add data to chart from custom post types metadata’ is closed to new replies.