在ASP .NET MVC 5图表中的每个条形图上显示标签

时间:2018-07-03 03:58:53

标签: c# asp.net-mvc charts

我想在图表的每个条上水平显示标签

@using GJob.Models.Views
@model BasicChart1
@{
    ViewBag.Title = "The Chart";
    Layout = "~/Views/Shared/_Layout2.cshtml";
}

@{
    var myChart = new Chart(width: 1200, height: 800)
        .AddTitle("My chart title")
        .AddSeries("Default",
                    chartType: "bar",
                    markerStep: 1,
                   xValue: Model.XData, xField: "Name",
                   yValues: Model.YDataINT, yFields: "Value")
        .Write("png");
}

我试图从这些文章中寻求帮助

https://docs.microsoft.com/en-us/aspnet/web-pages/overview/data/7-displaying-data-in-a-chart

https://weblogs.asp.net/imranbaloch/chart-helper-in-asp-net-mvc-3-0-with-transparent-background

https://forums.asp.net/t/1654046.aspx?Chart+Helper+need+all+xvalue+labels+not+just+a+few

以及图表的构造函数的定义

namespace System.Web.Helpers
{
    //
    // Summary:
    //     Displays data in the form of a graphical chart.
    public class Chart

但不知道如何做。

请帮助。谢谢!

更新1

我认为我们已经解决了 ChartArea

<asp:Chart ID="Chart1" runat="server" Width="600" Height="400" DataSourceID="SqlDataSource1">
    <series>
        <asp:Series Name="Series1" XValueMember="Country" YValueMembers="Column1" 
            ChartType="Column">
        </asp:Series>
    </series>
    <chartareas>
        <asp:ChartArea BackColor="NavajoWhite" BackGradientStyle="LeftRight" 
            Name="ChartArea1" ShadowOffset="5">
            <AxisY Title="Number of Customers">
            </AxisY>
            <AxisX Title="Countries" IsLabelAutoFit="True">
                <LabelStyle Angle="-90" Interval="1" />
            </AxisX>
            <Area3DStyle Enable3D="True" />
        </asp:ChartArea>
    </chartareas>
</asp:Chart>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
    SelectCommand="SELECT COUNT(*),Country FROM Customers GROUP BY Country">
</asp:SqlDataSource>

但是目前尚不清楚在ASP.NET MVC中配置Chart类的语法。

1 个答案:

答案 0 :(得分:0)

所以我在这里https://www.danylkoweb.com/Blog/simplified-aspnet-mvc-charts-A5

找到了正确的方法

控制器

jfile <- stream_in(file("file.json"))

查看

     public ActionResult PercentageDistributionFault()
            {
                // Get data to use in Chart
                var model = new BasicChart1();
                var data = db.RequestMalfunctionTypes.GroupBy(x => x.MalfunctionTypeID).Select(x => new NameCountClass
                {
                    Name = x.FirstOrDefault().MalfunctionType.Name,
                    Count = x.Count()
                }).OrderBy(x => x.Count).ToList();

                model.Data.AddRange(data);

                foreach (var item in data)
                {
                    model.XData.Add(item.Name);
                    model.YDataINT.Add(item.Count);
                }
                // Create chart object and pass it to View
                var chart = new Chart(1200, 800, GetMyCustomTheme())
                   .AddTitle("Mega chart")
                   .AddSeries("Default",
                               chartType: "bar",
                              xValue: model.XData, xField: "Name",
                              yValues: model.YDataINT, yFields: "Value");

                return View(chart);
            }

private string GetMyCustomTheme()
        {
            return @"
            <Chart BackColor=""White"" BackGradientStyle=""TopBottom"" BorderColor=""181, 64, 1"" 
               BorderWidth=""2"" BorderlineDashStyle=""Solid"" Palette=""SemiTransparent"" 
               AntiAliasing=""All"">

               <ChartAreas>
                  <ChartArea Name=""Default"" _Template_=""All"" BackGradientStyle=""None""
                     BackColor=""White"" BackSecondaryColor=""White"" 
                     BorderColor=""64, 64, 64, 64"" BorderDashStyle=""Solid"" 
                     ShadowColor=""Transparent"">                      

                     <AxisX Title=""Countries"" IsLabelAutoFit=""True"">
                        <LabelStyle Angle = ""-90"" Interval = ""1"" />   
                     </AxisX>

                     <Area3DStyle Enable3D=""False"" Inclination=""60"" Rotation=""45""/>
                  </ChartArea>
               </ChartAreas>
            </Chart>";
        }

因此答案是使用此行@using System.Web.Helpers @model Chart @{ ViewBag.Title = "Mega Chart"; Layout = "~/Views/Shared/_Layout2.cshtml"; } @{ Model.Write(format: "png"); }