Superstar

Grafana Apache Echarts - Change line style

Nov 2nd, 2023
915
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function transformData(data, types) {
  2.   const processedColors = {};
  3.  
  4.   data.forEach((item, index) => {
  5.       // Extracts color property from the item
  6.       const { color } = item;
  7.      
  8.       if (processedColors[color]) {
  9.           const previousTypeIndex = types.indexOf(processedColors[color]);
  10.  
  11.           let nextItemIndex = previousTypeIndex + 1;
  12.           if (types.length > nextItemIndex) {
  13.             item.lineStyle = types[nextItemIndex] || '';
  14.           } else {
  15.             item.lineStyle = '';
  16.           }
  17.           processedColors[color] = item.lineStyle;
  18.       } else {
  19.           item.lineStyle = types[0];
  20.           processedColors[color] = types[0];
  21.       }
  22.   });
  23.  
  24.   return data;
  25. }
  26.  
  27. let data = [
  28.   {
  29.     color: 'green'
  30.   },
  31.   {
  32.     color: 'green'
  33.   },
  34.   {
  35.     color: 'green'
  36.   },
  37.   {
  38.     color: 'green'
  39.   }
  40. ];
  41.  
  42. let types = ['solid', 'dashed', 'dotted'];
  43.  
  44. data = transformData(data, types);
  45.  
  46. console.log(data);
  47.  
Advertisement
Add Comment
Please, Sign In to add comment