javascript - Dygraphs Change each series color - Stack Overflow

I have a Line chart done with dygraphs, all the data is added using a Datatable so It has a variable nu

I have a Line chart done with dygraphs, all the data is added using a Datatable so It has a variable number of series and data points.

All I want to do is to specify each series color. I know I can use an array in the constructor like this.

colors:
            ["#7FFF00", "#00FFFF", "#008080", "#00BFFF", "#FFD700", "#FF69B42", "#20B2AA",
             "#FF0000", "#FFFF00", "#FF1493", "#000080", "#00FF00", "#6B8E23", "#00FA9A",
             "#B0C4DE", "#F0E68C", "#DAA520"]
        ,

But as I said before the number of series is variable and unknown. I know I can update the options of a specific series like this:

g.updateOptions({
    'S1001': {
        strokeWidth: 10,
        drawPoints: true,
        pointSize: 4,
        highlightCircleSize: 6,

    }
});

Where S1001 is the name of the series, but I cant find any option to change its color.

How can I specify a series color if I know its Series ID?

Thanks in advance, Pablo

EDIT: Well I found a workaround that works quite well. I can change the colour of a series by setting the value in the "colorsMap_" and then redrawing the graph so:

g.colorsMap_["SeriesID"] = "#FFFFFF";

g.updateOptions("Any option so the graph is redrawn");

Is there any way of redrawing or refreshing the graph that not includes calling updateOptions?

thanks!

I have a Line chart done with dygraphs, all the data is added using a Datatable so It has a variable number of series and data points.

All I want to do is to specify each series color. I know I can use an array in the constructor like this.

colors:
            ["#7FFF00", "#00FFFF", "#008080", "#00BFFF", "#FFD700", "#FF69B42", "#20B2AA",
             "#FF0000", "#FFFF00", "#FF1493", "#000080", "#00FF00", "#6B8E23", "#00FA9A",
             "#B0C4DE", "#F0E68C", "#DAA520"]
        ,

But as I said before the number of series is variable and unknown. I know I can update the options of a specific series like this:

g.updateOptions({
    'S1001': {
        strokeWidth: 10,
        drawPoints: true,
        pointSize: 4,
        highlightCircleSize: 6,

    }
});

Where S1001 is the name of the series, but I cant find any option to change its color.

How can I specify a series color if I know its Series ID?

Thanks in advance, Pablo

EDIT: Well I found a workaround that works quite well. I can change the colour of a series by setting the value in the "colorsMap_" and then redrawing the graph so:

g.colorsMap_["SeriesID"] = "#FFFFFF";

g.updateOptions("Any option so the graph is redrawn");

Is there any way of redrawing or refreshing the graph that not includes calling updateOptions?

thanks!

Share edited Sep 11, 2013 at 11:43 pablito asked Sep 11, 2013 at 7:35 pablitopablito 812 silver badges7 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

I came here while trying to solve this exact problem so I am adding a more up to date answer in the hope it helps someone else.

As of Dygraphs 2.0 (which contains breaking changes) it is possible to format individual series.

You can set in your options the following:

{
    series: {
        mySeriesName: {
            color: "green",    //Accepts CSS colour values.

            //There are other properties you can set too:
            strokeWidth: 3,
            drawPoints: true,
            pointSize: 5,
            highlightCircleSize: 7
        }
    }
}

Replace 'mySeriesName' with the name given to a series in your CSV file or labels array.

You can try reading the documentation about series options but I don't personally find it all that clear. This demo was more helpful to me.

Maybe too late in answering but @pablito is correct that there should be an options of directly changing the color using updateOptions. Currently, this feature is not supported so we have to live with hacks. If we use the hack

g.colorsMap_["SeriesID"] = "#FFFFFF";
g.updateOptions({});

as suggested above, it works but the problem is that Dygraph does not remember the changes once you zoom in or out. To make Dygraph remember the changes, implement the following

g.colorsMap_["SeriesID"] = "#FFFFFF";
g.updateOptions({});
g.user_attrs_.colors[index] = "#FFFFFF"; // Where index is index number of the SeriesID
Dygraph.updateDeep(g.user_attrs_, g.user_attrs_);

You should define a greater number of colours than you have series in the initial constructor: extra entries beyond the number of series are ignored. later, when you add series, the extra colours will be used. Obviously, you will need to decide on some maximum number of series you want to handle.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744880389a4598806.html

相关推荐

  • javascript - Dygraphs Change each series color - Stack Overflow

    I have a Line chart done with dygraphs, all the data is added using a Datatable so It has a variable nu

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信