带文本 x 轴的 graphael 条形图

发布于 2024-10-12 06:30:47 字数 363 浏览 5 评论 0原文

我想知道如何制作一个简单的条形图,该条形图可能以天为 x 轴,值为“今天”和“昨天”,y 轴可能为“时间”,对应值为“1”和“2” '。我想我对如何将文本设置为 x 轴的值、如何显示 y 轴以及 rgaxis 到底做什么感到困惑...... (我找到了一个使用 axis = rgaxis(0,300,400,0,500,8,2) 的示例,我只知道它是 xpos, ypos,<代码>宽度,<代码>??,<代码>??数字刻度,<代码>??)。任何见解都会很棒!或者具有更功能齐全的条形图示例(标签等)的页面。谢谢。

I was wondering how I can make a simple bar chart that perhaps has day as the x-axis, with values 'today' and 'yesterday', and the y-axis as perhaps 'time' with corresponding values '1' and '2'. I guess I'm confused as to how to set text as the values for the x-axis, how to show the y axis, and what exactly r.g.axis does...
(I found an example using axis = r.g.axis(0,300,400,0,500,8,2) and I only know it's the xpos, ypos,width, ??, ?? num ticks, ??). Any insight would be great! Or a page with more fully featured bar chart examples (labels, etc). Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

心凉怎暖 2024-10-19 06:30:47

为了所有那些谷歌搜索的人:

r.g.axis(x_start, y_start, x_width, from, to, steps, orientation, labels, type, dashsize)

x_starty_start:轴文本距左下角的距离

x_width:结束位置沿 x 轴的文本

fromto:用于指定要使用的范围,而不是使用标签参数

steps:是数字刻度数 - 1

方向:似乎指定 x 轴与 y 轴

类型:是使用的刻度线类型。

这都是从源代码推断出来的。我想我现在会切换到带有文档的图表库......

For the sake of all those googling this:

r.g.axis(x_start, y_start, x_width, from, to, steps, orientation, labels, type, dashsize)

x_start and y_start: distance of the axis text from the bottom left corner

x_width: position of the end of the text along the x axis

from and to: used to specify and range to use instead of using the labels argument

steps: is the number of ticks - 1

orientation: seems to specify x-axis vs. y-axis

type: is the type of tick mark used.

This was all deduced from the source code. I think I'll be switching to a charting library with documentation now...

揽月 2024-10-19 06:30:47

当前代码(Raphaeljs 2.0)已更改,必须稍作修改才能使用 Raphael.g.axis 而不是 rgaxis:

Raphael.g.axis(85,230,310,null,null,4,2,["今天", "昨天",
"明天", "未来"], "|", 0, r)

The current code (Raphaeljs 2.0) has changed and has to be slightly adapted to use Raphael.g.axis instead of r.g.axis:

Raphael.g.axis(85,230,310,null,null,4,2,["Today", "Yesterday",
"Tomorrow", "Future"], "|", 0, r)

青春如此纠结 2024-10-19 06:30:47

你走在正确的轨道上。您使用 g.axis ,用于设置文本的位置参数可在“文本”参数(位置)中找到,并使用“方向”参数切换 y。我在这里添加了一个示例,

带有文本 x 轴的条形图

You're on the right track. You use g.axis and the positional arguments for setting the text is found in the 'text' arg (positional) and for toggling the y using the 'orientation' args. I added an example here,

Barchart with text x-axis

绾颜 2024-10-19 06:30:47

阅读了这个问答和十几个类似的问答,我仍然无法让 gRaphaël 显示条形图的正确标签。这些食谱似乎都引用了该库的旧版本,或者不再存在的 github 页面。 gRaphaël 产生了一些漂亮的输出——但它的文档还有很多不足之处。

然而,我能够结合使用 Firebug 和 Inspect This Element 来跟踪代码并查看它生成的内容。深入研究条形图对象,所需的几何图形就在那里。为了避免其他人的挫败感,我解决了这个问题:

<script>

function labelBarChart(r, bc, labels, attrs) {
    // Label a bar chart bc that is part of a Raphael object r
    // Labels is an array of strings. Attrs is a dictionary
    // that provides attributes such as fill (text color)
    // and font (text font, font-size, font-weight, etc) for the
    // label text.

    for (var i = 0; i<bc.bars.length; i++) {
        var bar = bc.bars[i];
        var gutter_y = bar.w * 0.4;
        var label_x = bar.x
        var label_y = bar.y + bar.h + gutter_y;
        var label_text = labels[i];
        var label_attr = { fill:  "#2f69bf", font: "16px sans-serif" };

        r.text(label_x, label_y, label_text).attr(label_attr);
    }

}


// what follows is just setting up a bar chart and calling for labels
// to be applied

window.onload = function () {
    var r = Raphael("holder"),
        data3 = [25, 20, 13, 32, 15, 5, 6, 10],
        txtattr = { font: "24px 'Allerta Stencil', sans-serif", fill: "rgb(105, 136, 39)"};
    r.text(250, 10, "A Gratuitous Chart").attr(txtattr);
    var bc = r.barchart(10, 10, 500, 400, data3, {
            stacked: false,
            type: "soft"});
    bc.attr({fill: "#2f69bf"});
    var x = 1;

    labelBarChart(r, bc,
                 ['abc','b','card','d','elph','fun','gurr','ha'],
                 { fill:  "#2f69bf", font: "16px sans-serif" }
            );

};
</script>
<div id="holder"></div>

您可以对 labelBarChart() 进行一些小的清理,但这基本上可以完成工作。

Reading this Q&A and a dozen like it, I still could not get gRaphaël to show proper labels for a bar chart. The recipes all seemed to refer to older versions of the library, or to github pages that are no longer there. gRaphaël produces some great looking output--but its docs leave much to be desired.

I was, however, able to use a combination of Firebug and Inspect This Element to follow the code and see what it produced. Diving into the barchart object, the required geometry is right there. To save others the frustration, here's how I solved the problem:

<script>

function labelBarChart(r, bc, labels, attrs) {
    // Label a bar chart bc that is part of a Raphael object r
    // Labels is an array of strings. Attrs is a dictionary
    // that provides attributes such as fill (text color)
    // and font (text font, font-size, font-weight, etc) for the
    // label text.

    for (var i = 0; i<bc.bars.length; i++) {
        var bar = bc.bars[i];
        var gutter_y = bar.w * 0.4;
        var label_x = bar.x
        var label_y = bar.y + bar.h + gutter_y;
        var label_text = labels[i];
        var label_attr = { fill:  "#2f69bf", font: "16px sans-serif" };

        r.text(label_x, label_y, label_text).attr(label_attr);
    }

}


// what follows is just setting up a bar chart and calling for labels
// to be applied

window.onload = function () {
    var r = Raphael("holder"),
        data3 = [25, 20, 13, 32, 15, 5, 6, 10],
        txtattr = { font: "24px 'Allerta Stencil', sans-serif", fill: "rgb(105, 136, 39)"};
    r.text(250, 10, "A Gratuitous Chart").attr(txtattr);
    var bc = r.barchart(10, 10, 500, 400, data3, {
            stacked: false,
            type: "soft"});
    bc.attr({fill: "#2f69bf"});
    var x = 1;

    labelBarChart(r, bc,
                 ['abc','b','card','d','elph','fun','gurr','ha'],
                 { fill:  "#2f69bf", font: "16px sans-serif" }
            );

};
</script>
<div id="holder"></div>

There are a bunch of little cleanups you could do to labelBarChart(), but this basically gets the job done.

沙与沫 2024-10-19 06:30:47

这是我编写的用于添加标签的函数。它不是特别优雅,但它会添加标签:

Raphael.fn.labelBarChart = function(x_start, y_start, width, labels, textAttr) {
  var paper = this;

  // offset width and x_start for bar chart gutters
  x_start += 10;
  width -= 20;

  var labelWidth = width / labels.length;

  // offset x_start to center under each column
  x_start += labelWidth / 2;

  for ( var i = 0, len = labels.length; i < len; i++ ) {
    paper.text( x_start + ( i * labelWidth ), y_start, labels[i] ).attr( textAttr );
  }
};

用法如下:

var paper = Raphael(0, 0, 600, 400);
var chart = paper.barchart(0, 0, 600, 380, [[63, 86, 26, 15, 36, 62, 18, 78]]);

var labels = ['Col 1', 'Col 2', 'Col 3', 'Col 4', 'Col 5', 'Col 6', 'Col 7', 'Col 8'];
paper.labelBarChart(0, 390, 600, labels, {'font-size': 14});

Here's a function I wrote for adding the labels. It's not particularly elegant but it will add the labels:

Raphael.fn.labelBarChart = function(x_start, y_start, width, labels, textAttr) {
  var paper = this;

  // offset width and x_start for bar chart gutters
  x_start += 10;
  width -= 20;

  var labelWidth = width / labels.length;

  // offset x_start to center under each column
  x_start += labelWidth / 2;

  for ( var i = 0, len = labels.length; i < len; i++ ) {
    paper.text( x_start + ( i * labelWidth ), y_start, labels[i] ).attr( textAttr );
  }
};

Usage is as follows:

var paper = Raphael(0, 0, 600, 400);
var chart = paper.barchart(0, 0, 600, 380, [[63, 86, 26, 15, 36, 62, 18, 78]]);

var labels = ['Col 1', 'Col 2', 'Col 3', 'Col 4', 'Col 5', 'Col 6', 'Col 7', 'Col 8'];
paper.labelBarChart(0, 390, 600, labels, {'font-size': 14});
看透却不说透 2024-10-19 06:30:47

我想提出

这导致代码:

<script>

function labelBarChart(r, bc, labels, attrs) {
    // Label a bar chart bc that is part of a Raphael object r
    // Labels is an array of strings. Attrs is a dictionary
    // that provides attributes such as fill (text color)
    // and font (text font, font-size, font-weight, etc) for the
    // label text.
    //Added test : replace bc.bars by generic variable barsRef 
    var barsRef = (typeof bc.bars[0].length === 'undefined') ? bc.bars : bc.bars[0];

    var bar, gutter_y, label_x, label_y, label_text;
    //Added consideration of set attrs (if set)
    var label_attr = (typeof attrs === 'undefined') ? {} : attrs;
    label_attr['fill'] = (typeof label_attr['fill'] === 'undefined') ? "#2f69bf" : label_attr['fill'];
    label_attr['font'] = (typeof label_attr['font'] === 'undefined') ? "16px sans-serif" : label_attr['font'];

    for (var i = 0; i<barsRef.length; i++) {
        bar = barsRef[i];
        gutter_y = bar.w * 0.4;
        label_x = bar.x
        label_y = bar.y + bar.h + gutter_y;
        label_text = labels[i];
        r.text(label_x, label_y, label_text).attr(label_attr);
    }
}


// what follows is just setting up a bar chart and calling for labels
// to be applied
// I added an array of data to illustrate : data4
window.onload = function () {
    var r = Raphael("holder"),
        data3 = [25, 20, 13, 32, 15, 5, 6, 10],
        data4 = [0, 2, 1, 40, 1, 65, 46, 11],
        txtattr = { font: "24px 'Allerta Stencil', sans-serif", fill: "rgb(105, 136, 39)"};
    r.text(250, 10, "A Gratuitous Chart").attr(txtattr);
    var bc = r.barchart(10, 10, 500, 400, [data3, data4] {
            stacked: true,
            type: "soft"});
    bc.attr({fill: "#2f69bf"});

    labelBarChart(r, bc,
                 ['abc','b','card','d','elph','fun','gurr','ha'],
                 { fill:  "#2f69bf", font: "16px sans-serif" }
            );

};
</script>
<div id="holder"></div>

我刚刚用两个堆叠的值数组对其进行了测试。

I would like to propose a solution of an issue of the labelBarChart function proposed by Jonathan Eunice.
considering stacked bar-graphes (or other bar-graphes with more than one array of values), I added a test on bc.bars[0] in case the bc.bars.length means the number of arrays of values stacked.

This lead to the code :

<script>

function labelBarChart(r, bc, labels, attrs) {
    // Label a bar chart bc that is part of a Raphael object r
    // Labels is an array of strings. Attrs is a dictionary
    // that provides attributes such as fill (text color)
    // and font (text font, font-size, font-weight, etc) for the
    // label text.
    //Added test : replace bc.bars by generic variable barsRef 
    var barsRef = (typeof bc.bars[0].length === 'undefined') ? bc.bars : bc.bars[0];

    var bar, gutter_y, label_x, label_y, label_text;
    //Added consideration of set attrs (if set)
    var label_attr = (typeof attrs === 'undefined') ? {} : attrs;
    label_attr['fill'] = (typeof label_attr['fill'] === 'undefined') ? "#2f69bf" : label_attr['fill'];
    label_attr['font'] = (typeof label_attr['font'] === 'undefined') ? "16px sans-serif" : label_attr['font'];

    for (var i = 0; i<barsRef.length; i++) {
        bar = barsRef[i];
        gutter_y = bar.w * 0.4;
        label_x = bar.x
        label_y = bar.y + bar.h + gutter_y;
        label_text = labels[i];
        r.text(label_x, label_y, label_text).attr(label_attr);
    }
}


// what follows is just setting up a bar chart and calling for labels
// to be applied
// I added an array of data to illustrate : data4
window.onload = function () {
    var r = Raphael("holder"),
        data3 = [25, 20, 13, 32, 15, 5, 6, 10],
        data4 = [0, 2, 1, 40, 1, 65, 46, 11],
        txtattr = { font: "24px 'Allerta Stencil', sans-serif", fill: "rgb(105, 136, 39)"};
    r.text(250, 10, "A Gratuitous Chart").attr(txtattr);
    var bc = r.barchart(10, 10, 500, 400, [data3, data4] {
            stacked: true,
            type: "soft"});
    bc.attr({fill: "#2f69bf"});

    labelBarChart(r, bc,
                 ['abc','b','card','d','elph','fun','gurr','ha'],
                 { fill:  "#2f69bf", font: "16px sans-serif" }
            );

};
</script>
<div id="holder"></div>

I just tested it with 2 arrays of values stacked.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文