Karan
Karan

Reputation: 268

Apply different colors to different words according to their weight using Highcharts API

I am trying to solve a issue with Highcharts wordcloud and I am not sure how I can accomplish this. Using the example provided by Highcharts for their wordcloud I want different color for words according to their weight. So let us say If I have a words who's weight/repetition is greater than 2 I want those words to be colored green. I came up with this piece of code but it does not work.

 var chart = Highcharts.chart('container', {
          series: [{
              type: 'wordcloud',
              data: data,
              name: 'Occurrences'
          }],
          title: {
              text: 'Wordcloud of Lorem Ipsum'
          }
      });
      console.log(chart);

   for(var i = 0; i < data.length; i++) {
    if (chart.userOptions.series[0].data[i].weight > 2) {
        chart.userOptions.series[0].data[i].name.colors = ['green'];
     }
  }

Here is the Jsfiidle URL : http://jsfiddle.net/Lfznzsof/15/

Upvotes: 1

Views: 678

Answers (1)

Miroslav Savovski
Miroslav Savovski

Reputation: 2500

Please try this one:

var lines = text.split(/[,\. ]+/g),
data = Highcharts.reduce(lines, function (arr, word) {
    var obj = Highcharts.find(arr, function (obj) {
        return obj.name === word;
    });
    if (obj) {
        obj.weight += 1;
        if(obj.weight > 2){
          obj.color= "green";
        }
    } else {
        obj = {
            name: word,
            weight: 1,
            //color: "red"
        };
        arr.push(obj);
    }
    return arr;
}, []);

console.log(data);
var chart = Highcharts.chart('container', {
          series: [{
              type: 'wordcloud',
              data: data,
              name: 'Occurrences'
          }],
          title: {
              text: 'Wordcloud of Lorem Ipsum'
          }
      });
console.log(chart);

Here is JSFiddle: http://jsfiddle.net/Lfznzsof/81/
Hope this helps!

Upvotes: 1

Related Questions