2014-05-14 270 views
0

管窺 「PiePartial.cshtml」:繪製多個圖表視圖中的

@model IEnumerable<QLCLDA.Models.Chart.ChartValue> 

<script type="text/javascript"> 
    var chart; 
    var legend; 

    AmCharts.ready(function() { 
     // PIE CHART 
     chart = new AmCharts.AmPieChart(); 
     chart.dataProvider = @Html.Raw(Json.Encode(Model)); 
     chart.titleField = "name"; 
     chart.valueField = "value"; 
     chart.outlineColor = "#FFFFFF"; 
     chart.outlineAlpha = 0.8; 
     chart.outlineThickness = 2; 

     // WRITE 
     chart.write("piediv"); 
    }); 
</script> 

Index.cshtml

@model QLCLDA.Models.Chart.ChartDraw 

@{ 
    ViewBag.Title = "Index"; 
} 

<script src="~/Scripts/amcharts/amcharts.js"></script> 
<script src="~/Scripts/amcharts/pie.js"></script> 

@{ 
    Html.RenderPartial("PiePartial", Model.ProjectCount); 
    Html.RenderPartial("PiePartial", Model.IsFailed); 
} 

<div id="piediv" style="width: 100%; height: 400px;"></div> 

<div id="piediv">調用僅僅只顯示模式IsFailed 1點的圖表,我認爲他們有同樣的id="piediv"是它的原因。

我想說明2個餅圖與型號ProjectCountIsFailed內Index.cshtml

回答

1

實際上,你可以改變你的代碼,並直接在PartialView移動保持件容器:

管窺「餅圖.cshtml「:

@model Tuple<string, IEnumerable<QLCLDA.Models.Chart.ChartValue>> 
@{ 
    var controlId = Model.First; 
} 
<div id="@controlId" style="width: 100%; height: 400px;"></div> 

<script type="text/javascript"> 
    var chart; 
    var legend; 

    AmCharts.ready(function() { 
     // PIE CHART 
     chart = new AmCharts.AmPieChart(); 
     chart.dataProvider = @Html.Raw(Json.Encode(Model.Second)); 
     chart.titleField = "name"; 
     chart.valueField = "value"; 
     chart.outlineColor = "#FFFFFF"; 
     chart.outlineAlpha = 0.8; 
     chart.outlineThickness = 2; 

     // WRITE 
     chart.write("@controlId"); 
    }); 
</script> 

Index.cshtml

@model QLCLDA.Models.Chart.ChartDraw 

@{ 
    ViewBag.Title = "Index"; 

    var ProjectCountData = new Tuple<string, IEnumerable<QLCLDA.Models.Chart.ChartValue>>("ProjectCount", Model.ProjectCount); 

    var IsFailedData = new Tuple<string, IEnumerable<QLCLDA.Models.Chart.ChartValue>>("ProjectCount",IsFailed); 
} 

<script src="~/Scripts/amcharts/amcharts.js"></script> 
<script src="~/Scripts/amcharts/pie.js"></script> 

@{ 
    Html.RenderPartial("PiePartial", ProjectCountData); 
    Html.RenderPartial("PiePartial", IsFailedData); 
} 

在這種情況下,您將創建圖表的邏輯與實際的應用程序邏輯分離開來,您也可以根據需要添加任意數量的圖表。

+0

感謝您的幫助,我解決了這個問題! 我寫錯了部分視圖的名稱,正確的名稱是「PiePartial.cshtml」,但無所謂,我創建了一個像你一樣的變量'controlId'並傳遞了一個隨機字符串:) –

+0

我很高興聽到你'已經解決了你的問題! 但實際上最好發送不隨機controlId,但一些正確的價值。 –