How can I color dots in a xy scatterplot according to column value?

后端 未结 6 940
萌比男神i
萌比男神i 2020-12-23 13:35

Consider the following worksheet:

     A       B        C        D
1 COMPANY  XVALUE   YVALUE   GROUP
2 Apple     45       35       red
3 Xerox     45                


        
6条回答
  •  别那么骄傲
    2020-12-23 13:48

    I see there is a VBA solution and a non-VBA solution, which both are really good. I wanted to propose my Javascript solution.

    There is an Excel add-in called Funfun that allows you to use javascript, HTML and css in Excel. It has an online editor with an embedded spreadsheet where you can build your chart.

    I have written this code for you with Chart.js:

    https://www.funfun.io/1/#/edit/5a61ed15404f66229bda3f44

    To create this chart, I entered my data on the spreadsheet and read it with a json file, it is the short file.

    I make sure to put it in the right format, in script.js, so I can add it to my chart:

    var data = [];
    var color = [];
    var label = [];
    
    for (var i = 1; i < $internal.data.length; i++)
    {
        label.push($internal.data[i][0]);
        data.push([$internal.data[i][1], $internal.data[i][2]]);
        color.push($internal.data[i][3]);
    }
    

    I then create the scatter chart with each dot having his designated color and position:

     var dataset = [];
      for (var i = 0; i < data.length; i++) {   
        dataset.push({
          data: [{
            x: data[i][0],
            y: data[i][1] 
          }],
          pointBackgroundColor: color[i],
          pointStyle: "cercle",
          radius: 6  
        });
      }
    

    After I've created my scatter chart I can upload it in Excel by pasting the URL in the funfun Excel add-in. Here is how it looks like with my example:

    Once this is done You can change the color or the position of a dot instantly, in Excel, by changing the values in the spreadsheet.

    If you want to add extra dots in the charts you just need to modify the radius of data in the short json file.

    Hope this Javascript solution helps !

    Disclosure : I’m a developer of funfun

提交回复
热议问题