2017-08-01 61 views
-2

在我的数据库我已存储的颜色为一个字符串,我需要将这些颜色转换为十六进制和实施饼图上。有没有一个好的方法来做到这一点?
这是我的javascript代码。有两个数组(系列和颜色)存储数据。如何在饼图中实现数据库中的颜色?

<script type="text/javascript"> 
    $(function() { 
     $.ajax({ 
      url: 'getItems', 
      dataType: "json", 
      type: "GET", 
      contentType: 'application/json; charset=utf-8', 
      async: false, 
      processData: false, 
      cache: false, 
      delay: 15, 
      success: function (data) { 

       var series = new Array(); 
       var colors = new Array(); 
       for (var i in data) { 
        var serie = new Array(data[i].name, data[i].y, data[i].color); 

        series.push(serie); 
        colors.push(serie[data[i].color]); 

       } 
       DrawPieChart(series,colors); 
      }, 
      error: function (xhr) { 
       alert('error'); 
      } 
     }); 
    }); 

    function DrawPieChart(series,colors) { 
     console.log(series,colors); 
     Highcharts.chart('container', { 

      chart: { 
       plotBackgroundColor: null, 
       plotBorderWidth: 1, 
       plotShadow: false, 


      }, 
      title: { 
       text: ' Vehicle Summary' 
      }, 
      tooltip: { 
       pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>' 
      }, 
      plotOptions: { 
       pie: { 
        allowPointSelect: true, 
        cursor: 'pointer', 
        dataLabels: { 
         enabled: false 
        }, 
        showInLegend: true 
       } 
      }, 

      colors: [colors], 
      series: [{ 

       type: 'pie', 
       name: 'Task Status', 
       data: series 
      }] 
     }); 
    } 
    </script> 

这是我的方法,它从数据库过程中的项目的值称为“百分比”。

public ActionResult getItems() 
    { 
     List<itemViewModel> result = new List<itemViewModel>(); 
     result = unitOfWork.ExecuteSP<itemViewModel>("[percentage]"); 

     var res = result.OfType<string>(); 

     foreach (var dr in res) 
     { 
      itemViewModel summary = new itemViewModel(); 
      summary.name = dr[0].ToString().Trim(); 
      summary.y = Convert.ToDecimal(dr[1]); 
      // color 
      result.Add(summary); 

     } 

     return Json(result, JsonRequestBehavior.AllowGet); 
    } 

此外,这是我的模型

public class itemViewModel 
{ 
    public string name { get; set; } 
    public decimal? y { get; set; } 
    public string color { get; set; } 

} 
+3

到目前为止,你尝试过什么!你应该拿出代码!当我是一个tyro并做同样的事情时,我得到的评论比这更糟糕。请拿出你已经尝试 – Mandy8055

+1

https://stackoverflow.com/questions/2109756/how-do-i-get-the-color-from-a-hexadecimal-color-code-using-net – Peter

+1

什么图表控件你使用? - 你在做什么:Winforms,WPF,ASP ..? __Always__正确标记您的问题! – TaW

回答

0

您可以通过设置饼图的颜色属性的表达做到这一点,通过将一系列属性 - >填写--->点击FX并给出表达。即使我已经给你这个soln,但我不认为你的方法是正确的

你正在追随的是不是最好的方法不需要在数据库中存储颜色,只需从其他颜色表达式计算它值

+0

但我没有使用表单来创建饼图 – emmy

+0

你是如何创建饼图的? – Ashu

+0

我使用了高图(https://www.highcharts.com/demo/pie-basic)。在JavaScript中,我做了ajax调用来从数据库(名称和百分比)检索数据,我做了。现在我已经将数据库中的颜色存储为十六进制,并且我不知道如何在模型中声明它并通过该方法处理,以便为该颜色着色该饼图。该代码与http://www.c-sharpcorner.com/uploadfile/1f3f2a/charting-in-mvc/非常相似。 – emmy