R中的Dygraphs在浏览器中查看时不显示颜色

时间:2015-07-10 19:43:13

标签: javascript r graphics dygraphs

我正在使用" dygraphs"在R中打包以绘制条形图并为所有条形指定绿色。当我在R中显示图表时,事情是完全正常的,但当我尝试在Web浏览器中显示它们时,所有条形都变黑。这是代码(包括Javascript的剪辑,因为Google说我可以使用它来使用dygraph绘制条形图):

library(xts)
library(dygraphs)
library(lubridate)
library(dplyr)

df  <- data.frame(date = as.Date(13800:13810, origin = "1970-01-01"),
                  n    = 1:11)

graph_data <- xts(x = 1:10, order.by = as.Date(13801:13810, origin = "1970-01-01"))

dygraph(graph_data) %>%
    dyOptions(useDataTimezone = TRUE,
              colors = c("008000"),
              plotter = 
                  "function barChartPlotter(e) {
            var ctx = e.drawingContext;
            var points = e.points;
            var y_bottom = e.dygraph.toDomYCoord(0);  

            var bar_width = 1/2 * (points[1].canvasx - points[0].canvasx);
            ctx.fillStyle = e.color;

            for (var i = 0; i < points.length; i++) {
            var p = points[i];
            var center_x = p.canvasx;  // center of the bar

            ctx.fillRect(center_x - bar_width / 2, p.canvasy,
            bar_width, y_bottom - p.canvasy);
            ctx.strokeRect(center_x - bar_width / 2, p.canvasy,
            bar_width, y_bottom - p.canvasy);
            }
            }")

我可能不得不使用dygraph来绘制条形图,因为它可以快速绘制数千个条形图...

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

在设置颜色时,您只是缺少'#'。检查 colors = c("008000")行。它应该是colors = c("#008000")