简体   繁体   English

Echarts:我的饼图挡住了我的折线图,而我打算将它们绑定在一起

[英]Echarts:My pie charts block my line chart while I'm intended to bind them together

How can I bind pie chart and line chart together rather than appear one by one?如何将饼图和折线图绑定在一起,而不是一一出现? And the pie charts which appear later than line chart will block the line chart.并且出现在折线图之后的饼图会遮挡折线图。 Is there any chance the pie and line can appear together in the end?馅饼和线最后有没有机会一起出现? The current situation is that at the beginning , and then .目前的情况是,一开始然后

This is the JS code.这是JS代码。

    var dom2 = document.getElementById('demo');
    var chart = echarts.init(dom2);

    var option = {
        title: {
            text: '中药与疾病'
        },
        tooltip: {},
        legend: {
            data: ['中药', '疾病']
        },
        xAxis: {
            data: []
        },
        yAxis: [
            {},
            {}
        ],
        series: [
            {
                name: '中药',
                type: 'line',
                data: [],
                yAxisIndex: 0
            },
            {
                name: '疾病',
                type: 'line',
                data: [],
                yAxisIndex: 1
            }
        ]

    }

    chart.setOption(option);


    $.get('https://gist.githubusercontent.com/Linya-gzl/4d4f388e1b0e3d8e05c38f875b94a97c/raw/8c121acbfaf4aac9eccaf6b81cd1b3614203c185/demo1.json').done(function (data) {
        dataArr = JSON.parse(data);
        console.log(dataArr);
        chart.setOption({
            xAxis: {
                data: dataArr.map(row => row['categories'])
            },
            series: [{
                name: '中药',
                data: dataArr.map(row => row['value1'])
            },
            {
                name: '疾病',
                data: dataArr.map(row => row['value2'])
            }]
        });

        function buildPieSeries() {
            var len = dataArr.length;
            for (var i = 0; i < len; i++) {
                option.series.push({
                    type: 'pie',
                    radius: 15,
                    center: [110 + 90 * i, dataArr[i].value2 - 100],
                    label: {
                        show: true,
                        textStyle: {
                            fontSize: 8
                        }
                    },
                    data: [
                        { value: dataArr[i].value1, name: '黄连' },
                        { value: dataArr[i].value2, name: '黄芩' },
                    ]
                })
            }                
            chart.setOption(option, true);

        }
        setTimeout(buildPieSeries, 1000);


    });

and

<script src="https://cdnjs.cloudflare.com/ajax/libs/echarts/4.7.0/echarts.min.js" integrity="sha256-eKrx6Ly6b0Rscx/PSm52rJsvK76RJyv18Toswq+OLSs=" crossorigin="anonymous"></script>
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>
<div id="demo" style="width: 600px;height:400px;"></div>

I changed your code a bit in the series insertion part, by my opinion need inserting series completely because partial inserts sometimes cause problems with merging data.我在系列插入部分更改了您的代码,我认为需要完全插入系列,因为部分插入有时会导致合并数据出现问题。 Also I fixed coordinate calculation, more correct way take the already calculated coordinates from line if they the same.我还修复了坐标计算,如果它们相同,更正确的方法是从线中获取已经计算的坐标。

 document.addEventListener("DOMContentLoaded", e => { var targetNode = document.querySelector('#chartNode'); var chartInstance = echarts.init(targetNode); var option = { title: { text: '中药与疾病' }, tooltip: {}, legend: { data: ['中药', '疾病'] }, xAxis: { data: [] }, yAxis: [ {}, {} ], series: [ { name: '中药', type: 'line', data: [], yAxisIndex: 0 }, { name: '疾病', type: 'line', data: [], yAxisIndex: 1 } ] } chartInstance.setOption(option); $.get('https://gist.githubusercontent.com/Linya-gzl/4d4f388e1b0e3d8e05c38f875b94a97c/raw/8c121acbfaf4aac9eccaf6b81cd1b3614203c185/demo1.json').done(function (data) { dataArr = JSON.parse(data); chartInstance.setOption({ xAxis: { data: dataArr.map(row => row['categories']) }, series: [{ name: '中药', data: dataArr.map(row => row['value1']) }, { name: '疾病', data: dataArr.map(row => row['value2']) }]}); pieSeries = chartInstance.getOption().series; function buildPieSeries() { var len = dataArr.length; for (var i = 0; i < len; i++) { pieSeries.push({ type: 'pie', radius: 15, z: 10, center: chartInstance.getModel().getSeriesByName('中药')[0].getData().getItemLayout(i), // center: [110 + 90 * i, dataArr[i].value2 - 100], label: { show: true, textStyle: { fontSize: 8 }}, data: [ { value: dataArr[i].value1, name: '黄连' }, { value: dataArr[i].value2, name: '黄芩' }, ] }) }; chartInstance.setOption({ series: pieSeries }); } setTimeout(() => buildPieSeries(), 1000); }); });
 <script src="https://cdn.jsdelivr.net/npm/echarts@4.7.0/dist/echarts.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="chartNode" style="width: 600px;height:400px;"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM