Search code examples
javascriptjqueryjqplot

Jqplot, split an dynamic diagram


i have an diagram that have 12 values and i want to split it in 3 diagram that have 4 values each diagram. So i can have 4 diagrams. The list of arrays is dynamic.

$(document).ready(function(){
        var s1 = [2, 6, 7, 10,2, 6, 7, 10,2, 6, 7, 10];
        var s2 = [7, 5, 3, 2,7, 5, 3, 2,7, 5, 3, 2];
        var s3 = [14, 9, 3, 8,14, 9, 3, 8,14, 9, 3, 8];
  	    var s4 = [15, 8, 2, 10,15, 8, 2, 10,15, 8, 2, 10,];

        plot3 = $.jqplot('chart3', [s1, s2, s3, s4], {
            stackSeries: true,
            captureRightClick: true,
            seriesDefaults:{
                renderer:$.jqplot.BarRenderer,
                rendererOptions: {
                    highlightMouseDown: true   
                },
                pointLabels: {show: true}
            },
            legend: {
                show: true,
                location: 'e',
                placement: 'outside'
            }      
        });
     
        $('#chart3').bind('jqplotDataRightClick', 
            function (ev, seriesIndex, pointIndex, data) {
                $('#info3').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
            }
        ); 
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/jquery.jqplot.min.js"></script>

<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.pointLabels.min.js"></script>

<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.barRenderer.min.js"></script>

<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.dateAxisRenderer.min.js"></script>

<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.categoryAxisRenderer.min.js"></script>

<script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.enhancedPieLegendRenderer.min.js"></script>

    <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/jquery.jqplot.min.css" />

<div  id="chart3"></div>

And the question is how can i do this ? i have an array that looks like:

 array(0 => array(0 => array(0 => 2,1 => 6,2 => 7,3 => 10)),//this will be the first diagram
       1 => array(0 => array(0 => 7,1 => 5,2 => 3,3 => 2)),// this the second       
       2 => array(0 => array(0 => 14,1 => 9,2 => 3,3 => 14)),//this the third
       3 => array(0 => array(0 => 15,1 => 8,2 => 2,3 => 10))//this the fourth
       );

The list is dynamic. Can someone help me with that ?


Solution

  • You need to iterate over your array and create a chart based on each sub-array data like below:-

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
    <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/jquery.jqplot.min.js"></script>
    
    <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.pointLabels.min.js"></script>
    
    <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.barRenderer.min.js"></script>
    
    <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.dateAxisRenderer.min.js"></script>
    
    <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.categoryAxisRenderer.min.js"></script>
    
    <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/plugins/jqplot.enhancedPieLegendRenderer.min.js"></script>
    
    <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqPlot/1.0.9/jquery.jqplot.min.css" />
    
    <?php
    $array = array(
                0 => array(
                        0 => array(
                            2, 6, 7, 10,2, 6, 7, 10,2, 6, 7, 10
                    )
                ),
                1 => array(
                        0 => array(
                            7, 5, 3, 2,7, 5, 3, 2,7, 5, 3, 22
                    )
                ),
                2 => array(
                        0 => array(
                            14, 9, 3, 8,14, 9, 3, 8,14, 9, 3, 8
                    )
                ),3 => array(
                        0 => array(
                            15, 8, 2, 10,15, 8, 2, 10,15, 8, 2, 10,
                        )
                ),4 => array(
                        0 => array(
                            12, 18, 2, 0,1, 8, 2, 7,6, 8, 2, 21,
                        )
                )
           );
    
    
    foreach($array as $key=> $arr){?>
    <div  id="chart<?php echo $key;?>"></div>
    <script type="text/javascript">
    
    $(document).ready(function(){
    
            plot3 = $.jqplot('chart<?php echo $key;?>', <?php echo json_encode($arr); ?>, {
                stackSeries: true,
                captureRightClick: true,
                seriesDefaults:{
                    renderer:$.jqplot.BarRenderer,
                    rendererOptions: {
                        highlightMouseDown: true   
                    },
                    pointLabels: {show: true}
                },
                legend: {
                    show: true,
                    location: 'e',
                    placement: 'outside'
                }      
            });
    
            $('#chart<?php echo $key;?>').bind('jqplotDataRightClick', 
                function (ev, seriesIndex, pointIndex, data) {
                    $('#info3').html('series: '+seriesIndex+', point: '+pointIndex+', data: '+data);
                }
            ); 
        });
    
    </script>
    
    <?php } ?>