【问题标题】:Multiple Shape annotations on D3plus line plotD3plus 线图上的多个形状注释
【发布时间】:2021-09-29 04:44:21
【问题描述】:

我有一个使用 d3plus 创建的线图,我需要有多个带有Shape 标签的线注释(一个水平线和两个垂直线)来识别它们。我能够让线条正确显示,但我无法让多个标签显示在图表上。

这是我的一组数据:

[
  {"id":"line","myDate":"Sep 2011","value":8303269},
  {"id":"line","myDate":"Jul 2012","value":8389066},
  {"id":"line","myDate":"Sep 2012","value":8632844},
  {"id":"line","myDate":"Mar 2013","value":8926414},
  {"id":"line","myDate":"Jun 2013","value":9169985},
  {"id":"line","myDate":"Mar 2014","value":9273689},
  {"id":"line","myDate":"Sep 2014","value":9343712},
  {"id":"line","myDate":"Dec 2014","value":9416974},
  {"id":"line","myDate":"May 2015","value":9546380},
  {"id":"line","myDate":"Sep 2015","value":10484320},
  {"id":"line","myDate":"Sep 2015","value":11455165},
  {"id":"line","myDate":"Dec 2015","value":11997581},
  {"id":"line","myDate":"Apr 2016","value":12104931},
  {"id":"line","myDate":"May 2016","value":12111915},
  {"id":"line","myDate":"Jun 2016","value":12127119},
  {"id":"line","myDate":"Jul 2016","value":12800226},
  {"id":"line","myDate":"Mar 2017","value":12915303},
  {"id":"line","myDate":"Nov 2017","value":12947360},
  {"id":"line","myDate":"Nov 2018","value":12957309}
]

这是我的 LinePlot annotations 数组。

            new LinePlot()
                .select("#demo")
                .height(500)
                .config({
                    data: vm.plotData,
                    x: 'myDate',
                    y: 'value',
                    groupBy: 'id',
                    annotations: [
                        {
                            data: [
                                {id: "start", x: "Jul 2012", y: 8000000},
                                {id: "start", x: "Jul 2012", y: 20000000},
                                {id: "end", x: "Nov 2017", y: 8000000},
                                {id: "end", x: "Nov 2017", y: 20000000},
                                {id: "dotted", x: "Sep 2011", y: this.item.ceilingValue},
                                {id: "dotted", x: "Nov 2018", y: this.item.ceilingValue}
                            ],
                            shape: "Line",
                            stroke: function(d) {
                                return d.id === "box" ? "blue" : "green";
                            },
                            strokeDasharray: "10",
                            strokeWidth: 2
                        },
                        {
                            data: [
                                {
                                    x: 'Jul 2012',
                                    y: 20000000,
                                    width: 100,
                                    height: 25
                                }
                            ],
                            fill: "#0c1971",
                            label: "Start Date",
                            labelConfig: {
                                textAnchor: "middle",
                                verticalAlign: "middle"
                            },
                            shape: "Rect"
                        },
                        {
                            data: [
                                {
                                    x: 'Nov 2017',
                                    y: 20000000,
                                    width: 10,
                                    height: 25
                                }
                            ],
                            fill: "#255832",
                            label: "End Date",
                            labelConfig: {
                                textAnchor: "middle",
                                verticalAlign: "middle"
                            },
                            shape: "Rect"
                        }
                    ]
                })
                .render()

发生的情况是,当我只有 Start Date 项目时,它工作正常,但当我添加 End Date 对象时,第一个消失了,第二个没有完全呈现。

根据文档,annotations 接受

Shape 类的自定义配置对象,可以是单个配置对象或一组配置对象。`,

这是我提供的,所以我不确定问题出在哪里。我需要进行哪些更改才能让我的所有标签正确显示?

【问题讨论】:

    标签: d3.js plot d3plus


    【解决方案1】:

    我能够根据this comment 弄清楚。要点是您必须将特定形状的所有项目组合到一个对象中:

                        annotations: [
                            {
                                data: [
                                    {id: "start", x: "Jul 2012", y: 8000000},
                                    {id: "start", x: "Jul 2012", y: 20000000},
                                    {id: "end", x: "Nov 2017", y: 8000000},
                                    {id: "end", x: "Nov 2017", y: 20000000},
                                    {id: "dotted", x: "Sep 2011", y: this.item.ceilingValue},
                                    {id: "dotted", x: "Nov 2018", y: this.item.ceilingValue}
                                ],
                                shape: "Line",
                                stroke: function(d) {
                                    return d.id === "box" ? "blue" : "green";
                                },
                                strokeDasharray: "10",
                                strokeWidth: 2
                            },
                            {
                                data: [
                                    {
                                        id: 'start',
                                        label: 'Start Date',
                                        x: 'Jul 2012',
                                        y: 20000000,
                                        width: 100,
                                        height: 25
                                    },
                                    {
                                        id: 'end',
                                        label: 'End Date',
                                        x: 'Nov 2017',
                                        y: 20000000,
                                        width: 100,
                                        height: 25
                                    }
                                ],
                                fill: function(d) {
                                    let result;
                                    switch (d.id) {
                                        case 'start':
                                            result = "#0c1971";
                                            break;
                                        case 'end':
                                            result = "#255832";
                                            break;
                                    }
                                    return result;
                                },
                                label: function (d) {
                                    let result;
                                    switch (d.id) {
                                        case 'start':
                                            result = "Start Date";
                                            break;
                                        case 'end':
                                            result = "End Date";
                                            break;
                                        }
                                        return result;
                                    },
                                labelConfig: {
                                    textAnchor: "middle",
                                    verticalAlign: "middle"
                                },
                                shape: "Rect"
                            }
                        ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 2020-08-26
      • 1970-01-01
      • 2017-10-18
      • 2014-10-16
      相关资源
      最近更新 更多