美文网首页
甘特图(二)添加标注线和坐标位置

甘特图(二)添加标注线和坐标位置

作者: 小北呀_ | 来源:发表于2019-11-26 14:09 被阅读0次
如图所示:
1.echarts 下载
npm install echarts --save
2.main.js引入 echarts
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
3.页面代码:
<template>
    <div>
        <div class="index">
            <div style="width:100%;height:100%;" id="activity_gantt"></div>
            <p class='title' :style="{top:offsetY+ 'px',left:offsetX + 'px'}">{{title}}</p>
        </div>
    </div>
</template>
<script>
    export default {
        name: '',
        data() {
            return {
                offsetX:'',
                offsetY:'',
                title:''
            }
        },
        created() {
            this.$nextTick(() => {
                //动态数据 post_data我自己写了一个。
                let post_data = {
                    start_date: "2019/07/01",
                    stop_date: "2019/07/31",
                    act_list:[
                        {
                            start:"2019/07/10",
                            stop:"2019/07/20",
                            title:'活动A'
                        },
                        {
                            start:"2019/07/12",
                            stop:"2019/07/16",
                            title:'活动B'
                        },
                        {
                            start:"2019/07/02",
                            stop:"2019/07/12",
                            title:'活动C'
                        },
                    ],
                    x_list: ["2019/07/01", "2019/07/02", "2019/07/03", "2019/07/04", "2019/07/05", "2019/07/06", "2019/07/07",
                        "2019/07/08", "2019/07/09", "2019/07/10", "2019/07/11", "2019/07/12", "2019/07/13", "2019/07/14",
                        "2019/07/15", "2019/07/16", "2019/07/17", "2019/07/18", "2019/07/19", "2019/07/20", "2019/07/21",
                        "2019/07/22", "2019/07/23", "2019/07/24", "2019/07/25", "2019/07/26", "2019/07/27", "2019/07/28",
                        "2019/07/29", "2019/07/30", "2019/07/31"
                    ]
                }
                //计算两个日期相差天数
                function DateDiff(sDate1, sDate2) {
                    //sDate1和sDate2是2006-12-18格式
                    var aDate, oDate1, oDate2, iDays;
                    aDate = sDate1.split("/");
                    oDate1 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]);
                    aDate = sDate2.split("/");
                    oDate2 = new Date(aDate[1] + '/' + aDate[2] + '/' + aDate[0]);
                    iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);//把相差的毫秒数转换为天数
                    return iDays;
                }

                let start_ = post_data.start_date
                let start_list = []
                let end_list = []
                let index_list = []
                post_data.act_list.forEach((value, index) => {
                    index_list.push(index)
                    start_list.push(DateDiff(start_, value.start))
                    end_list.push(DateDiff(value.start, value.stop))
                })


                this.$echarts.init(document.getElementById('activity_gantt')).dispose();//销毁前一个实例
                let ganttOptionChart = this.$echarts.init(document.getElementById('activity_gantt'));
                let ganttOption = {
                    backgroundColor:'rgb(34,44,61)',
                    //设置canvas内部表格的内距
                    grid: {
                        x: 35,
                        y: 5,
                        x2: 40,
                        y2: 30,
                    },
                    xAxis: {
                        type: 'value',
                        // 坐标网格
                        splitLine: {
                            show:true,
                            lineStyle:{
                                width:0.6,
                                color: 'rgba(255,255,255,0.2)',
                                type:'dashed'
                            }
                        },
                        //设置x轴坐标线的样式
                        axisLine: {
                            lineStyle: {
                                type: 'solid',
                                color: 'rgba(255,255,255,0.3)',//x轴坐标线的颜色
                                width:'1'//x轴坐标线的宽度
                            }
                        },
                        //x轴刻度数值颜色
                        axisLabel: {
                            textStyle: {
                                color: 'rgba(255,255,255,0.3)'
                            },
                            formatter: function (value, index) {
                                // 显示 2019/07/01
                                return post_data.x_list[value]
                            },
                        },
                    },
                    yAxis: {
                        data: [],
                        // 坐标网格
                        splitLine: {
                            show:true,
                            lineStyle: {
                                width: 0.6,
                                color: 'rgba(255,255,255,0.2)',
                                type: 'dashed'
                            }
                        },
                        //y轴刻度线
                        axisTick: {
                            show: false
                        },
                        //y轴坐标线
                        axisLine: {
                            show: false,
                        },
                        //y轴刻度值
                        axisLabel: {
                            show: false
                        },
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器,坐标轴触发有效
                            type: 'shadow'        // 默认为直线,可选为:'line' | 'shadow'
                        },
                        formatter: function (params) {}
                    },
                    series: [
                        {
                            barWidth: 8,//柱图宽度
                            type: 'bar',
                            stack: '总量',
                            itemStyle: {
                                normal: {
                                    barBorderColor: 'rgba(0,0,0,0)',
                                    color: 'rgba(0,0,0,0)'
                                },
                                emphasis: {
                                    barBorderColor: 'rgba(0,0,0,0)',
                                    color: 'rgba(0,0,0,0)'
                                }
                            },
                            data: [],
                        },
                        {
                            barWidth: 8,//柱图宽度
                            //每个项目 持续时间长度
                            type: 'bar',
                            stack: '总量',
                            label: {
                                normal: {//柱形图上面显示的文字
                                    show: false,
                                    position: 'inside'
                                }
                            },
                            data: [],
                            itemStyle: {
                                normal: {
                                    // 柱形图颜色
                                    color: '#14E09B'
                                },
                            },
                            //标注线
                            markLine: {
                                data:[],
                                lineStyle: {
                                    normal: {
                                        color: '#fff',
                                    }
                                },
                                label: {
                                    normal: {
                                        fontWeight: 'bold',
                                        padding: 2,
                                        fontSize: 14,
                                        fontFamily: 'Microsoft YaHei',
                                    },
                                },
                            }
                        }
                    ]
                }


                //塞数据
                ganttOption.yAxis.data = index_list
                ganttOption.series[0].data = start_list
                ganttOption.series[1].data = end_list


                ganttOptionChart.setOption(ganttOption);
                window.addEventListener("resize", () => {
                    ganttOptionChart.resize()
                })
                ganttOptionChart.on('click', (params) => {
                    // 坐标
                    this.offsetX = params.event.offsetX - 20
                    this.offsetY = params.event.offsetY - 60
                    let index = Number(params.name)
                    // 活动标题
                    this.title = post_data.act_list[index].title
                    // 添加甘特图的标注线
                    let start_x = '0',stop_x = '0'
                    post_data.x_list.forEach(function (value,valueIndex) {
                        if (value === post_data.act_list[index].start) {
                            start_x = valueIndex
                        }
                        if (value === post_data.act_list[index].stop) {
                            stop_x = valueIndex
                        }
                    })
                    ganttOption.series[1].markLine.data = [
                        //一个活动的开始x轴
                        [
                            {  xAxis: start_x, yAxis: 'min', symbol: 'circle' },
                            {  xAxis: start_x, yAxis: 'max', symbol: 'circle' }
                        ],
                        //一个活动的结束x轴
                        [
                            {  xAxis: stop_x, yAxis: 'min', symbol: 'circle' },
                            {  xAxis: stop_x, yAxis: 'max', symbol: 'circle' }
                        ]
                    ]
                    ganttOptionChart.setOption(ganttOption);

                })
            })
        },
    }
</script>
<style>
    .index{
        width: 700px;height: 300px;margin: 30px;position: relative
    }
    .title{
        width: 50px;
        height: 20px;
        background: #fff;
        color: #333333;
        border-radius: 4px;
        padding: 3px;
        position: absolute;
    }
</style>

标注线主要代码在series好热点击事件里。

相关文章

  • 甘特图(二)添加标注线和坐标位置

    如图所示: 1.echarts 下载 2.main.js引入 echarts 3.页面代码: 标注线主要代码在s...

  • 绘图

    添加图例标签 标题和XY坐标标签 text()文本和annotation()箭头标注

  • 228. Summary Ranges

    还是两个指针start和end标注起始位置,要注意坐标不要越界

  • 高德云图 iOS SDK 使用总结

    APP开发需求:加载地图,定位到用户当前的位置,并标注附近的坐标点(上图中的蓝色标注点)。坐标数据由业务人员提供。...

  • CAD看图王标坐标切换坐标系

    CAD看图王里面提供测坐标和标注坐标功能,可以方便在CAD图纸上测量标注坐标,但有时图纸中的坐标和软件测量的坐标会...

  • 十七 图形图像开发中的3D数学

    坐标系 左手坐标系和右手坐标系 世界坐标系:经纬坐标原点在本初子午线与赤道的交点 物体坐标系和物体的位置的所处的方...

  • matplotlib 学习

    基本语法 加一条线 坐标轴设置 移动坐标轴 legend 图例 Annotation 标注 散点图 柱状图 8.等...

  • 读书/分享《余华全集》

    - 您在位置 #8560-8563的标注 | 添加于 2016年11月8日星期二 上午8:11:06在这里,作者有...

  • kindle 摘录

    失控 (KK系列) (凯文•凯利) 您在位置 #94-95的标注 | 添加于 2016年5月17日星期二 上午9:...

  • 百度地图API (1):往地图中添加标注点

    1.实现功能:通过给定的坐标点,往地图中添加标注点,点击标注点,显示改点的详细信息。2.效果图:演示链接:http...

网友评论

      本文标题:甘特图(二)添加标注线和坐标位置

      本文链接:https://www.haomeiwen.com/subject/snbewctx.html