201Echarts - 自定义系列(Cycle Plot)

简介: 201Echarts - 自定义系列(Cycle Plot)
效果图

源代码
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <!-- 引入 echarts.js -->
    <script src="js/echarts.min.js"></script>
    <script src="js/jquery-1.11.0.min.js"></script>
    <script src="dist/extension/dataTool.js"></script>
  </head>
  <body>
    <!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="main" style="width: 1024px;height:768px;"></div>
    <script type="text/javascript">
      // 基于准备好的dom,初始化echarts实例
      var myChart = echarts.init(document.getElementById('main'));
      var option;
var rawData = [
    [2002, 14, 21, 25, 21, 26, 32, 27, 20, 10, 11, 5, 5],
    [2003, 18, 24, 28, 24, 33, 37, 30, 25, 13, 14, 6, 6],
    [2004, 22, 31, 36, 28, 37, 43, 35, 30, 13, 13, 7, 7],
    [2005, 25, 32, 38, 34, 39, 48, 38, 29, 14, 14, 8, 8],
    [2006, 29, 38, 47, 33, 44, 57, 41, 39, 16, 16, 9, 8],
    [2007, 29, 35, 49, 34, 43, 57, 41, 37, 20, 17, 9, 10],
    [2008, 22, 32, 37, 30, 35, 44, 38, 31, 16, 17, 8, 7],
    [2009, 25, 34, 41, 33, 39, 47, 44, 32, 17, 17, 9, 8],
    [2010, 26, 35, 46, 40, 47, 61, 47, 41, 20, 18, 9, 10],
    [2011, 29, 39, 55, 38, 55, 67, 53, 41, 19, 20, 11, 11],
    [2012, 38, 48, 60, 49, 57, 79, 62, 54, 26, 26, 13, 11]
];
var dataByMonth = [];
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
echarts.util.each(rawData, function (entry, yearIndex) {
    echarts.util.each(entry, function (value, index) {
        if (index) {
            var monthIndex = index - 1;
            var monthItem = dataByMonth[monthIndex] = dataByMonth[monthIndex] || [];
            monthItem[0] = monthIndex;
            monthItem[yearIndex + 1] = value;
        }
    });
});
var averageByMonth = [];
echarts.util.each(dataByMonth, function (entry, index) {
    var sum = 0;
    echarts.util.each(entry, function (value, index) {
        index && (sum += value);
    });
    averageByMonth.push([index, sum / (entry.length - 1)]);
});
function renderTrendItem(params, api) {
    var categoryIndex = api.value(0);
    var unitBandWidth = api.size([0, 0])[0] * 0.85 / (rawData.length - 1);
    var points = echarts.util.map(rawData, function (entry, index) {
        var value = api.value(index + 1);
        var point = api.coord([categoryIndex, value]);
        point[0] += unitBandWidth * (index - rawData.length / 2);
        return point;
    });
    return {
        type: 'polyline',
        shape: {
            points: points
        },
        style: api.style({
            fill: null,
            stroke: api.visual('color'),
            lineWidth: 2
        })
    };
}
function renderAverageItem(param, api) {
    var categoryIndex = api.value(0);
    var bandWidth = api.size([0, 0])[0] * 0.85;
    var point = api.coord([api.value(0), api.value(1)]);
    return {
        type: 'line',
        shape: {
            x1: point[0] - bandWidth / 2,
            x2: point[0] + bandWidth / 2,
            y1: point[1],
            y2: point[1]
        },
        style: api.style({
            fill: null,
            stroke: api.visual('color'),
            lineWidth: 2
        })
    };
}
option = {
    tooltip: {
    },
    title: {
        text: 'Sales Trends by Year within Each Month',
        subtext: 'Sample of Cycle Plot',
        left: 'center'
    },
    legend: {
        top: 70,
        data: ['Trend by year (2002 - 2012)', 'Average']
    },
    dataZoom: [{
        type: 'slider',
        showDataShadow: false,
        bottom: 10,
        height: 20,
        borderColor: 'transparent',
        backgroundColor: '#e2e2e2',
        handleIcon: 'M10.7,11.9H9.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4h1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7v-1.2h6.6z M13.3,22H6.7v-1.2h6.6z M13.3,19.6H6.7v-1.2h6.6z', // jshint ignore:line
        handleSize: 20,
        handleStyle: {
            shadowBlur: 6,
            shadowOffsetX: 1,
            shadowOffsetY: 2,
            shadowColor: '#aaa'
        },
        labelFormatter: ''
    }, {
        type: 'inside'
    }],
    grid: {
        bottom: 70,
        top: 120,
    },
    xAxis: {
        data: months
    },
    yAxis: {
        boundaryGap: [0, '20%']
    },
    series: [{
        type: 'custom',
        name: 'Average',
        renderItem: renderAverageItem,
        encode: {
            x: 0,
            y: 1
        },
        data: averageByMonth
    }, {
        type: 'custom',
        name: 'Trend by year (2002 - 2012)',
        renderItem: renderTrendItem,
        encode: {
            x: 0,
            y: echarts.util.map(rawData, function (entry, index) {
                return index + 1;
            })
        },
        data: dataByMonth
    }]
};
      myChart.setOption(option);
    </script>
  </body>
</html>
目录
相关文章
|
5天前
Echarts 热力图自定义开发
Echarts 热力图自定义开发
|
7月前
202Echarts - 自定义系列(Wind Barb)
202Echarts - 自定义系列(Wind Barb)
34 0
|
7月前
198Echarts - 自定义系列(Profile)
198Echarts - 自定义系列(Profile)
21 0
|
7月前
|
关系型数据库
197Echarts - 自定义系列(Polar Heatmap)
197Echarts - 自定义系列(Polar Heatmap)
25 0
|
7月前
193Echarts - 自定义系列(Custom Cartesian Polygon)
193Echarts - 自定义系列(Custom Cartesian Polygon)
27 0
|
5天前
|
JavaScript
echarts_自定义graph关系图
echarts_自定义graph关系图
22 0
|
5天前
|
前端开发 JavaScript 定位技术
Docusaurus框架——react+antd+echarts自定义mdx生成图表代码解释文档
Docusaurus框架——react+antd+echarts自定义mdx生成图表代码解释文档
28 0
|
7月前
195Echarts - 自定义系列(Error Scatter on Catesian)
195Echarts - 自定义系列(Error Scatter on Catesian)
12 0
|
7月前
191Echarts - 自定义系列(Custom Bar Trend)
191Echarts - 自定义系列(Custom Bar Trend)
21 0
|
6月前
|
JSON 前端开发 数据格式
Echarts饼状图数据交互请求ajax自定义颜色
Echarts饼状图数据交互请求ajax自定义颜色
24 0