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>
目录
相关文章
|
6月前
Echarts 热力图自定义开发
Echarts 热力图自定义开发
252 0
202Echarts - 自定义系列(Wind Barb)
202Echarts - 自定义系列(Wind Barb)
61 0
198Echarts - 自定义系列(Profile)
198Echarts - 自定义系列(Profile)
36 0
|
关系型数据库
197Echarts - 自定义系列(Polar Heatmap)
197Echarts - 自定义系列(Polar Heatmap)
59 0
193Echarts - 自定义系列(Custom Cartesian Polygon)
193Echarts - 自定义系列(Custom Cartesian Polygon)
50 0
|
6月前
|
JavaScript
echarts_自定义graph关系图
echarts_自定义graph关系图
110 0
|
5月前
|
JavaScript 前端开发 数据可视化
ECharts 雷达图案例001-自定义节点动画
使用ECharts创建自定义雷达图,通过JavaScript动态更新高亮和交互反馈,增强用户体验。关键步骤包括:开启动画效果,数据更新时保持图表状态,鼠标悬浮时动态高亮指标,优化动画性能。案例展示了ECharts在数据可视化中的灵活性和表现力。[查看完整案例](https://download.csdn.net/download/No_Name_Cao_Ni_Mei/89454380)。
188 0
 ECharts 雷达图案例001-自定义节点动画
|
5月前
|
计算机视觉
Echarts饼图,自定义饼图图例的排列方式, formatter使用语法
Echarts饼图,自定义饼图图例的排列方式, formatter使用语法
|
5月前
|
JSON JavaScript 定位技术
Echarts自定义地图显示区域,可以显示街道,小区,学校等区域
Echarts自定义地图显示区域,可以显示街道,小区,学校等区域
195Echarts - 自定义系列(Error Scatter on Catesian)
195Echarts - 自定义系列(Error Scatter on Catesian)
22 0
下一篇
无影云桌面