2014-04-07 77 views
1

我嘗試用jquery動態加載顏色。圖中的動態顏色

這是工作:

var colors_array= ["#9CC4E4", "#3A89C9", "#F26C4F"]; 

Morris.Donut({ 

    element: 'donut-example', 
    colors: colors_array, 
    data: [ 
    {label: "Download Sales", value: 12}, 
    {label: "In-Store Sales", value: 30}, 
    {label: "Mail-Order Sales", value: 20} 
    ] 
}); 

期望的結果(不工作):

function graphDonut(colors) { 
      var value = colors; 
      value = value.replace(/\|/g,'", "'); 
      var colors_array = '["' + value + '"]'; 

     Morris.Donut({ 

      element: 'donut-example', 
      colors: colors_array, 
      data: [ 
      {label: "Download Sales", value: 12}, 
      {label: "In-Store Sales", value: 30}, 
      {label: "Mail-Order Sales", value: 20} 
      ] 
     }); 

    } 

graphDonut("#9CC4E4|#3A89C9|#F26C4F"); 

回答

1

我認爲你需要

var colors_array = value.split("|"); 

更換

var colors_array = '["' + value + '"]'; 

它給了我這樣的輸出:

["#9CC4E4", "#3A89C9", "#F26C4F"] 

希望這有助於。

2

分割字符串或傳遞數組,後者會更容易

function graphDonut(colors) { 

    Morris.Donut({ 
     element: 'donut-example', 
     colors : colors, 
     data : [ 
      {label: "Download Sales", value: 12}, 
      {label: "In-Store Sales", value: 30}, 
      {label: "Mail-Order Sales", value: 20} 
     ] 
    }); 
} 

graphDonut(['#9CC4E4', '#3A89C9', '#F26C4F']); 

function graphDonut(colors) { 
    var arr = colors.split('|'); 

    Morris.Donut({ 
     element: 'donut-example', 
     colors : arr, 
     data : [ 
      {label: "Download Sales", value: 12}, 
      {label: "In-Store Sales", value: 30}, 
      {label: "Mail-Order Sales", value: 20} 
     ] 
    }); 
} 

graphDonut("#9CC4E4|#3A89C9|#F26C4F"); 
0

替換下面的代碼

Morris.Donut({ 
 

 
    element: 'donut-example', 
 
    colors: ["#9CC4E4", "#3A89C9", "#F26C4F"], 
 
    data: [ 
 
    {label: "Download Sales", value: 12}, 
 
    {label: "In-Store Sales", value: 30}, 
 
    {label: "Mail-Order Sales", value: 20} 
 
    ] 
 
});

我得到同樣的問題,我分享我的更正後的代碼如下

new Morris.Donut({ 
 
    element: "MyChart", 
 
    colors: ["#9CC4E4", "#3A89C9", "#F26C4F"], 
 
    data: object2 
 
    });

Do nut Chart Colorful view

0

試試這個:

<script> 
    new Morris.Donut({ 
     element: 'donut-example', 
     data: [ 
      {label: "Serie 1", value: 12}, 
      {label: "Serie 2", value: 30}, 
      {label: "Serie 3", value: 20}, 
      {label: "Serie 3", value: 20} 
     ], 
     colors: ['#a6d000', '#0070e7', '#e700b5', '#ffab17'], 
     xkey: 'y', 
     ykeys: ['vaue'] 
    }); 
</script>