同一页面上的多个堆叠图表

时间:2017-09-20 21:11:23

标签: amcharts

认为这很容易,但我错过了一些东西。尝试将两个堆叠的100%图表添加到同一页面。数据是通过php从MySQL提供的。我知道php代码是正确的,因为我能够在源代码中看到正确的数据。问题是,第二张图表没有出现。

我(我认为)已经确认图表没有任何冲突的ID。以下是两个图表的代码:

图表1(这个按预期显示):

<!-- Chart code -->
<script>
var ageChart = AmCharts.makeChart("agePie", {
    "type": "serial",
    "theme": "light",

    "dataProvider": [{
        "demographic": "National",
        "18to34": 30.14,
        "35to54": 40.14,
        "55to64": 13.40,
        "65over": 16.32

    },
    {
        "demographic": "Segment",
        "18to34": <?php echo $age1Avg ?>,
        "35to54": <?php echo $age2Avg ?>,
        "55to64": <?php echo $age3Avg ?>,
        "65over": <?php echo $age4Avg ?> 

    }

    ],
    "valueAxes": [{
        "stackType": "100%",
        "axisAlpha": 0,
        "gridAlpha": 0,
        "labelsEnabled": false,
        "position": "left"
    }],
    "graphs": [{
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "18-34",
        "type": "column",
        "valueField": "18to34"
    }, {
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "35-54",
        "type": "column",
        "valueField": "35to54"
    }, {
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "55-64",
        "type": "column",
        "valueField": "55to64"
    }, {
        "balloonText": "[[category]]:[[title]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "Over 65",
        "type": "column",
        "valueField": "65over"
    }],
    "marginTop": 30,
    "marginRight": 0,
    "marginLeft": 0,
    "marginBottom": 40,
    "autoMargins": false,
    "categoryField": "demographic",
    "categoryAxis": {
        "gridPosition": "start",
        "axisAlpha": 0,
        "gridAlpha": 0
    }

});
</script>

图表2(此图不显示)。应该是相同的配置,只是不同的数据比较。

<script>
var ethChart = AmCharts.makeChart("ethPie", {
    "type": "serial",
    "theme": "light",

    "dataProvider": [{
        "demographic2": "National",
        "Causacian": 30.14,
        "AfricanAmerican": 40.14,
        "Latin": 13.40,
        "Other": 16.32

    },
    {
        "demographic2": "Segment",
        "Causacian": <?php echo $eth1Avg ?>,
        "AfricanAmerican": <?php echo $eth2Avg ?>,
        "Latin": <?php echo $eth3Avg ?>,
        "Other": <?php echo $eth4Avg ?> 

    }

    ],
    "valueAxes": [{
        "stackType": "100%",
        "axisAlpha": 0,
        "gridAlpha": 0,
        "labelsEnabled": false,
        "position": "left"
    }],
    "graphs": [{
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "Caucasian",
        "type": "column",
        "valueField": "Caucasian"
    }, {
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "African American",
        "type": "column",
        "valueField": "AfricanAmerican"
    }, {
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "Latin",
        "type": "column",
        "valueField": "Latin"
    }, {
        "balloonText": "[[category]]:[[title]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "Other",
        "type": "column",
        "valueField": "Other"
    }],
    "marginTop": 30,
    "marginRight": 0,
    "marginLeft": 0,
    "marginBottom": 40,
    "autoMargins": false,
    "categoryField": "demographic2",
    "categoryAxis": {
        "gridPosition": "start",
        "axisAlpha": 0,
        "gridAlpha": 0
    }

});
</script>

按相应的ID调用每个图表:

<div id="agePie"></div>
<div id="ethPie"></div>

正确方向的想法或推动?

1 个答案:

答案 0 :(得分:0)

请运行下面的代码段。您的代码正在运行,似乎缺少定义的维度会导致问题。

我建议您尝试为两个图表设置一个CSS类,以防它们具有相同的尺寸。请查看以下示例:

.chartdiv {
  width: 100%;
  height: 300px;
}

将课程添加到DivElement

<div id="agePie" class="chartdiv"></div>
<div id="ethPie" class="chartdiv"></div>

确保清理浏览器缓存。这可以解释为什么你之前没有看到差异。

&#13;
&#13;
var ageChart = AmCharts.makeChart("agePie", {
    "type": "serial",
    "theme": "light",

    "dataProvider": [{
        "demographic": "National",
        "18to34": 30.14,
        "35to54": 40.14,
        "55to64": 13.40,
        "65over": 16.32

    },
    {
        "demographic": "Segment",
        "18to34": 24,
        "35to54": 40,
        "55to64": 60,
        "65over": 80 

    }

    ],
    "valueAxes": [{
        "stackType": "100%",
        "axisAlpha": 0,
        "gridAlpha": 0,
        "labelsEnabled": false,
        "position": "left"
    }],
    "graphs": [{
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "18-34",
        "type": "column",
        "valueField": "18to34"
    }, {
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "35-54",
        "type": "column",
        "valueField": "35to54"
    }, {
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "55-64",
        "type": "column",
        "valueField": "55to64"
    }, {
        "balloonText": "[[category]]:[[title]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "Over 65",
        "type": "column",
        "valueField": "65over"
    }],
    "marginTop": 30,
    "marginRight": 0,
    "marginLeft": 0,
    "marginBottom": 40,
    "autoMargins": false,
    "categoryField": "demographic",
    "categoryAxis": {
        "gridPosition": "start",
        "axisAlpha": 0,
        "gridAlpha": 0
    }

});


var ethChart = AmCharts.makeChart("ethPie", {
    "type": "serial",
    "theme": "light",

    "dataProvider": [{
        "demographic2": "National",
        "Causacian": 30.14,
        "AfricanAmerican": 40.14,
        "Latin": 13.40,
        "Other": 16.32

    },
    {
        "demographic2": "Segment",
        "Causacian": 23,
        "AfricanAmerican": 34,
        "Latin": 12,
        "Other": 3 

    }

    ],
    "valueAxes": [{
        "stackType": "100%",
        "axisAlpha": 0,
        "gridAlpha": 0,
        "labelsEnabled": false,
        "position": "left"
    }],
    "graphs": [{
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "Caucasian",
        "type": "column",
        "valueField": "Caucasian"
    }, {
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "African American",
        "type": "column",
        "valueField": "AfricanAmerican"
    }, {
        "balloonText": "[[title]], [[category]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "Latin",
        "type": "column",
        "valueField": "Latin"
    }, {
        "balloonText": "[[category]]:[[title]]<br><span style='font-size:14px;'><b>[[value]]</b></span>",
        "fillAlphas": 0.9,
        "fontSize": 11,
        "labelText": "[[title]]: [[value]]%",
        "lineAlpha": 0.5,
        "title": "Other",
        "type": "column",
        "valueField": "Other"
    }],
    "marginTop": 30,
    "marginRight": 0,
    "marginLeft": 0,
    "marginBottom": 40,
    "autoMargins": false,
    "categoryField": "demographic2",
    "categoryAxis": {
        "gridPosition": "start",
        "axisAlpha": 0,
        "gridAlpha": 0
    }

});
&#13;
.chartdiv {
  width: 100%;
  height: 300px;
}
&#13;
<script src="https://www.amcharts.com/lib/3/amcharts.js"></script>
<script src="https://www.amcharts.com/lib/3/serial.js"></script>
<script src="https://www.amcharts.com/lib/3/plugins/export/export.min.js"></script>
<link rel="stylesheet" href="https://www.amcharts.com/lib/3/plugins/export/export.css" type="text/css" media="all" />
<script src="https://www.amcharts.com/lib/3/themes/light.js"></script>

<div id="agePie" class="chartdiv"></div>
<div id="ethPie" class="chartdiv"></div>
&#13;
&#13;
&#13;