简体   繁体   English

在HTML / Javascript中自动将数字转换为彩色条

[英]Turning numbers into colored bars automatically in HTML/Javascript

I want to auto-generate a HTML table from some custom data. 我想从一些自定义数据自动生成HTML表。 One of the columns in my data is a number in the range 0-100, and I'd like to show it in a more graphical way, most desirably a colored horizontal bar. 我的数据中的一列是0-100范围内的数字,我想以更加图形化的方式显示它,最希望是彩色水平条。 The length of the bar would represent the value, and the color would also change (ie below 20 it's red, etc.) 条形的长度代表值,颜色也会改变(即低于20,它是红色等)

Something like this (created with paint.net): 像这样的东西(用paint.net创建):

替代文字
(source: thegreenplace.net ) (来源: thegreenplace.net

One way this can be achieved is by generating an appropriate .PNG and placing it there as an image. 可以实现的一种方法是通过生成适当的.PNG并将其作为图像放置在那里。 But I think that it can probably be done with some concoction of HTML/CSS/Javascript in an automatic way (ie the values thrown into the table are numeric, and JS converts them to bars before showing). 但我认为它可以用自动方式的一些HTML / CSS / Javascript混合来完成(即抛出到表中的值是数字,并且JS在显示之前将它们转换为条形)。

Perhaps someone has done something like this already and can share? 也许某人已经做过这样的事情并且可以分享?

Thanks in advance 提前致谢

PS: If it can work in IE6, that would be best (don't ask...) PS:如果它可以在IE6中工作,那将是最好的(不要问......)

PPS: It should work offline, so existing webservices (like Google charts) won't help PPS:它应该脱机工作,因此现有的Web服务(如Google图表)无济于事

AListApart has a great article on how to generate charts using purely CSS. AListApart有一篇关于如何使用纯CSS生成图表的精彩文章。 It's nice because it is accessible, meaning even without CSS it will provide meaningful data. 这很好,因为它是可访问的,这意味着即使没有CSS它也会提供有意义的数据。

http://www.alistapart.com/articles/accessibledatavisualization http://www.alistapart.com/articles/accessibledatavisualization

Update: According to one of the commenters on this answer, this solution will also work in IE6. 更新:根据这个答案的评论者之一,这个解决方案也适用于IE6。

This is doable. 这是可行的。

2 options: 2个选项:

1) put an image in every cell using the img tag and resize the image using the width attribute 1)使用img标签在每个单元格中放置图像,并使用width属性调整图像大小

2) put a div with a pre-set height and change the width according to the value you want it to display. 2)放置一个具有预设高度的div,并根据您想要显示的值更改宽度。 Use the background color of the div as your color - no images needed. 使用div的背景颜色作为颜色 - 无需图像。

example: 例:

<table style="border: 1px solid black">
<tr><th>name</th><th>value</th></tr>
<tr><td>hi</td><td><div style="height: 10px; width: 35px; background-color: #236611">35</div></td></tr>
<tr><td>yes</td><td><div style="height: 10px; width: 15px; background-color: #236611">15</div></td></tr>
<tr><td>see?</td><td><div style="height: 10px; width: 75px; background-color: #2366aa">75</div></td></tr>
</table>

... you could/should tweak the sizes to look nicer of course :-) ...你可以/应该调整大小,当然看起来更好:-)

The best way is the second part of simon's post. 最好的方法是西蒙的帖子的第二部分。 Place a div wherever you need it and change the width with javascript or PHP (depending on if you want it to dynamically change or not) using percentages. 在您需要的地方放置div,并使用百分比更改javascript或PHP的宽度(取决于您是否希望它动态更改)。 Use an if statement for the color. 对颜色使用if语句。 For ex, in javascript: 对于ex,在javascript中:

function displayGraph(barID, number)
{
    var color;
    if (number <= 20)
    {
        color = "red";
    }
    elseif (number > 20 && number <= 60)
    {
        color = "yellow";
    }
    else
    {
        color = "green";
    }

    var bar = document.getElementById(barID);
    bar.style.width = number + "%";
    bar.style.backgroundColor = color;
}

I didn't test this exactly, but something like it should work. 我没有完全测试这个,但它应该有用。

Check out the jQuery Sparkline which provides inline charts, similar to what you are looking for. 查看提供内联图表的jQuery Sparkline ,类似于您要查找的内容。 If you use a bullet graph , you can display the good/normal/bad ranges associated with your data which provides a huge amount of data in a very small space. 如果使用项目符号图 ,则可以显示与数据关联的正常/正常/不良范围,这些范围可在非常小的空间内提供大量数据。

Since you already have your data in a table, you might check out the jQuery Visualize Plugin . 由于您已经将数据放在表中,因此可以查看jQuery Visualize插件 Once you include it, you'd just call something like: 一旦你加入它,你只需要打电话:

$('table').visualize();

and it builds a graph from your table. 它会从你的表格中构建一个图表。

If you want it to work offline as well, maybe flot can be used. 如果您希望它也可以脱机工作,也许可以使用flot

It is based on canvas and jquery. 它基于canvas和jquery。

I haven't used it yet but it's on my todo list. 我还没有使用它,但它在我的待办事项清单上。

The sample code seems simple enough: 示例代码看起来很简单:

$(function () {
    var d1 = [[0, 3], [4, 8], [8, 5], [9, 13]];
    $.plot($("#placeholder"), [ d1 ]);
});

It's not HTML, but have you looked into Google Charts? 它不是HTML,但您是否查看了Google Charts? It's really quite amazing. 这真的很神奇。 http://code.google.com/apis/chart/ http://code.google.com/apis/chart/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM