简体   繁体   English

在 rails 中迭代 chartjs 的背景颜色数组

[英]iterate over an array of background colors for chartjs in rails

I'm using Rails to display a Chart.js graph of some data.我正在使用 Rails 来显示一些数据的 Chart.js 图。 My rails controller has an array called @colors that I pass to a JavaScript file called graphs.js.erb which gets displayed in a view template.我的 rails 控制器有一个名为@colors的数组,我将它传递给名为graphs.js.erb的 JavaScript 文件,该文件显示在视图模板中。 Everything is working fine there, my issue is that I have more data points than I do colors and I want to keep looping over the same colorsFromServer array to display however many colors I need for each datapoint.那里一切正常,我的问题是我的数据点多于颜色,我想继续循环访问相同的colorsFromServer数组以显示每个数据点需要的多种颜色。

So currently I'm using 6 colors in an array but I have 9 pieces of data to plot on a chart.所以目前我在一个数组中使用 6 种颜色,但我有 9 条数据要绘制在图表上。 I would like to just loop back to the first element in the colorsFromServer array and start pushing those colors into the color array.我只想循环回到colorsFromServer数组中的第一个元素,并开始将这些颜色推送到color数组中。

So ultimately with 9 points of data to plot the colors should be所以最终用 9 个数据点来绘制颜色应该是

  'rgba(255, 99, 132, 0.2)',
  'rgba(54, 162, 235, 0.2)',
  'rgba(255, 206, 86, 0.2)',
  'rgba(75, 192, 192, 0.2)',
  'rgba(153, 102, 255, 0.2)',
  'rgba(255, 159, 64, 0.2)'
   // keeps looping to get the 3 additional colors 
  'rgba(255, 99, 132, 0.2)',
  'rgba(54, 162, 235, 0.2)',
  'rgba(255, 206, 86, 0.2)'

In my Rails controller在我的 Rails 控制器中

@colors = [
  'rgba(255, 99, 132, 0.2)',
  'rgba(54, 162, 235, 0.2)',
  'rgba(255, 206, 86, 0.2)',
  'rgba(75, 192, 192, 0.2)',
  'rgba(153, 102, 255, 0.2)',
  'rgba(255, 159, 64, 0.2)'
]

Javascript Javascript

<% colors_json = @colors.to_json.html_safe %>
var colorsFromServer = <%= colors_json %>;
console.log("colorsFromServer");
console.log(colorsFromServer);

var colors = [];
var color;
for (var i = 0; i < article_data.length; i++) {
  color = colorsFromServer[i];
  colors.push(color);
}
console.log("COLORS");
console.log(colors);

the colors variable is what I'm using in Chart.js to draw the graph with certain background colors. colors变量是我在 Chart.js 中用来绘制具有特定背景颜色的图形的变量。 using the developer tools in Chrome you can see how the last 3 are undefined because the 6 colors have been already used, so I need a way to get back to the start of the colorsFromServer array to push more colors.使用 Chrome 中的开发人员工具,您可以看到最后 3 种颜色是如何未定义的,因为 6 种颜色已被使用,因此我需要一种方法来返回到 colorsFromServer 数组的开头以推送更多颜色。

在此处输入图片说明

you'll need to keep up with the color index separately from the data index.您需要与数据索引分开跟上颜色索引。
then reset to zero if it is greater than the number of colors available.如果它大于可用颜色的数量,则重置为零。

var colorsFromServer = <%= colors_json %>;
var colors = [];
var color;
var colorIndex = 0;
for (var i = 0; i < article_data.length; i++) {
  if (colorIndex >= colorsFromServer.length) {
    colorIndex = 0;
  }
  color = colorsFromServer[colorIndex];
  colorIndex++;
  colors.push(color);
}

I would do this, so you take the rest from integer division on the length of your array, it will never ecxeed the length.我会这样做,所以你从数组长度的整数除法中取出其余部分,它永远不会超过长度。

for (var i = 0; i < article_data.length; i++) {
  color = colorsFromServer[i % colorsFromServer.length];
  colors.push(color);
}

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

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