Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying my best to get rid of this pain when creating a Chart Plugin. I have registered custom post type to add every single chart with specific meta boxes. Everything works fine except the Datasets below.

datasets : [
    {
        fillColor : "'.$datasetfillcolor1.'",
        strokeColor : "'.$datasetstrokecolor1.'",
        data : ['.$datasetdata1.']
    },
    {
        fillColor : "'.$datasetfillcolor2.'",
        strokeColor : "'.$datasetstrokecolor2.'",
        data : ['.$datasetdata2.']
    },
]

I want to let my users create new datasets when they want, also the data sets should have a default parameter values when that aren't filled.

My shortcode looks like this:

`/* Register Shortcodes  */

add_action( 'init', 'register_chart_shortcodes');
function register_chart_shortcodes(){
   add_shortcode('chart', 'chart_function');
}

/* Chart Shortcode */

function chart_function($atts, $content = null) {

   extract(shortcode_atts(array(
      'id' => '',
   ), $atts));

    $chart_type = get_post_meta($id, 'chart_type', true);
    $height = get_post_meta($id, 'height', true);
    $width = get_post_meta($id, 'width', true);
    $labels = get_post_meta($id, 'labels', true);
    $datasetfillcolor1 = get_post_meta($id, 'datasetfillcolor1', true);
    $datasetfillcolor2 = get_post_meta($id, 'datasetfillcolor2', true);
    $datasetstrokecolor1 = get_post_meta($id, 'datasetstrokecolor1', true);
    $datasetstrokecolor2 = get_post_meta($id, 'datasetstrokecolor2', true);
    $datasetdata1 = get_post_meta($id, 'datasetdata1', true);
    $datasetdata2 = get_post_meta($id, 'datasetdata2', true);

   $return_string = '<canvas id="wpcp_'.$id.'" height="'.$height.'" width="'.$width.'"></canvas>

    <script>

        var barChartData'.$id.' = {
            labels : ['.$labels.'],
            datasets : [
                {
                    fillColor : "'.$datasetfillcolor1.'",
                    strokeColor : "'.$datasetstrokecolor1.'",
                    data : ['.$datasetdata1.']
                },
                {
                    fillColor : "'.$datasetfillcolor2.'",
                    strokeColor : "'.$datasetstrokecolor2.'",
                    data : ['.$datasetdata2.']
                },
            ]

        }

    var myLine = new Chart(document.getElementById("wpcp_'.$id.'").getContext("2d")). '.$chart_type.'(barChartData'.$id.');

</script>';

   return $return_string;
}

`

So my question is how can I let my users add datasets dynamically right from meta boxes of the post? So they can add as many datasets as they want. Also, how can I set default fillcolor, strokecolor, & data value in shortcode when users haven't defined them?

Thanks

share|improve this question
1  
I don't really see the question here? Could you clarify what it is you are actually trying to achieve? And while you're at it - could you also add what you've already tried? –  kraftner Jun 18 at 17:39
    
Hello @kraftner Thanks for your comment. I just edited my question and added: "So my question is how can I let my users add datasets dynamically right from meta boxes of the post? So they can add as many datasets as they want. Also, how can I set default fillcolor, strokecolor, & data value in shortcode when users haven't defined them?" Please let me know if you still need any clearance. Thanks –  Azizul Haque Jun 19 at 1:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.