【问题标题】:Raphael Line Chart Axis labels overlapping with Axis lines拉斐尔折线图轴标签与轴线重叠
【发布时间】:2013-07-24 13:39:28
【问题描述】:

为折线图轴绘制自定义标签时,标签旋转时标签的文本与轴重叠。如何解决这个问题?我尝试搜索文档并发现 Element.transform() 将用于旋转而不是 rotate() 因为它已贬值,但我无法使其工作。 有人可以帮忙吗?

   var lines1 = r.linechart(100, 50, 950, 470, x,
                             y, {
                                smooth : false,
                                symbol : 'circle',
                                axis : "0 0 1 1",
                                axisxstep: 20,
                                axisystep:20
                            }).hoverColumn(function () {
                                this.tags = r.set();
                                     for (var i = 0, ii = this.y.length; i < ii; i++) {
                                            this.tags.push(r.tag(this.x, this.y[i], this.values[i], 160, 5).insertBefore(this).attr([{ fill: "#fff" }, { fill: this.symbols[i].attr("fill") }]));
                                        }
                                    }, function () {
                                        this.tags && this.tags.remove();
                                    }); 

                         var axisItems = lines1.axis[0].text.items
                        for( var i = 0, l = axisItems.length; i < l; i++ ) {
                           var date = new Date(parseInt(axisItems[i].attr("text")));
                           axisItems[i].rotate(270,this.x ,this.y);
                           axisItems[i].attr("text", dateFormat(date, "dmmm,htt")); 
                        } 

【问题讨论】:

    标签: javascript raphael linechart axis-labels


    【解决方案1】:

    不确定你是否还需要这个......但我最终编写了自己的标签函数

    if (props.label !== undefined) {
        // don't use the default Raphael label() function, is broken
        chart.customLabel = customLabelFn;
        chart.customLabel(props.label);
    }
    

    customLabelFn 是我绘制标签的方式,这是我的垂直条形图示例:

    customLabelFn = function (labels) {
        var x, y;
        labels = labels || [];
    
        for (var i = 0; i < props.bars.length; i++) {
            x = props.bars[i].x;
            y = props.bars[i].y + props.bars[i].h + props.xaxisLabelOffset;
            props.r.text(x, y, labels[i]);
        }
        return this;
    };
    

    PS:在我调用 Raphael 生成图表后,我将条形推入了我的 props 变量中。

    // immediately calls the function so that we can have all the rendered bars
    chart.getBars = function() {
        props.bars = (props.stacked ?
            this.bars[0] : // pick the one and only bar
            this.bars);
    };
    chart.getBars();
    

    它对我有用,我可以访问其他任何地方的条形图,所以我可以使用它们在条形图、标签等顶部绘制数字。

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      在你内部for 循环使用: axisItems[i].translate(0,30);

      我还建议您旋转 90 而不是 270,这样文本将向下延伸而不是向上...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多