流程图:在图表渲染后如何更改样式选项(例如通过代码注入)?

发布于 2024-12-29 16:22:45 字数 1032 浏览 0 评论 0 原文

是否可以动态更改 Flot 图表组件的样式 – 通过控制台还是脚本 – 图表已渲染之后?即从 Flot 图表的内部配置(最初定义了图表的样式)外部的某个地方。

将我的问题转化为一个简单的用例:我有一个包含浮点图的页面,该图的条形颜色错误。使用 Web Inspector 的 JS 控制台,我想将条(线?)颜色从蓝色更改为棕色。 (而且我没有任何方法来编辑 jquery.flot.js 中的配置。)现在我希望从控制台注入代码,这可能会改变条形的颜色。

当我陷入困境时,我只希望得到基本代码结构的提示(不,我不是特别中级或高级用户)。

屏幕截图 1 – 渲染的图表,不受影响(箭头指示我要更改的内容):

屏幕截图 1,渲染的图表

… < a href="http://flot.googlecode.com/svn/trunk/API.txt" rel="nofollow noreferrer">Flot 的 API 说:

$.plot(placeholder, data, options)

但我不想设置/更改数据(已绘制出来),我只想更改其颜色表示从蓝色到棕色。我设法实现的改变某事的唯一效果是这段(非常无能的)代码:

$.plot("#chart", { series: {color: "#6C564C"} });

它将图表变成(屏幕截图2):

屏幕截图 2,更改了 Flot 图表样式

我不知道为什么图表消失了,而且我们不一定需要对此进行故障排除。我所寻找的只是通过代码注入来更改流程图选项的简单结构。

Is it possible to change the styling of a Flot chart's components on the fly – either via the console or a script – after the fact that the chart has been rendered? I.e from somewhere outside the Flot chart's internal configs (which initially defined the styling of the chart).

To put my question into a simple use case: I have a page containing a Flot chart which has the wrong color on the bars. Using Web Inspector's JS console, I want to alter the bars (lines?) color from blue to, say, brown. (And I don't have any means to edit the configs in jquery.flot.js.) Now I wish to inject code from the console, that might change the color of the bars.

I only wish to be hinted of a basic code structure as I'm stuck (nope, I'm not a particularly intermediate or advanced user).

Screenshot 1 – the rendered chart, unaffected (arrow indicates what I want to change):

Screenshot 1, the rendered chart

Flot's API says:

$.plot(placeholder, data, options)

But I don't want to set/change the data (which has already been plotted out), I just want to change its color representation from blue to, say, brown. The only effect of changing something, that I managed to achieve, was this (terribly clueless) piece of code:

$.plot("#chart", { series: {color: "#6C564C"} });

Which turned the chart into (Screenshot 2):

Screenshot 2, altered Flot chart style

I don't know why the chart disappeared, and we don't necessarily need to troubleshoot that. All I'm looking for is the simple structure for altering a Flot chart's options by means of code injection.

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

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

发布评论

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

评论(1

眼趣 2025-01-05 16:22:46

您可以通过更改数据对象并使用 setData() 来更改已绘制系列的颜色。这比再次调用 $.plot() 更快,但不会重新计算网格和刻度。如果您的实际数据点没有改变,这不是问题。

var data = [
    {
        label: "Your series",
        data: rawData,
        color: "#f00"
    }
]

// Initial draw:
var flotObject = $.plot("#chart", data, options);

// ...

// Change the colour:
data[0].color = "yellow"; // Change only the colour of the original data object
flotObject.setData(data);
flotObject.draw();

You can change the colour of an already-drawn series by changing the data object and using setData(). This is faster than calling $.plot() again but it won't recalculate the grid and ticks. If your actual data points don't change, this isn't an issue.

var data = [
    {
        label: "Your series",
        data: rawData,
        color: "#f00"
    }
]

// Initial draw:
var flotObject = $.plot("#chart", data, options);

// ...

// Change the colour:
data[0].color = "yellow"; // Change only the colour of the original data object
flotObject.setData(data);
flotObject.draw();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文