开发者

.NET Charting - Series Template

开发者 https://www.devze.com 2023-03-06 06:46 出处:网络
The Mission: Apply a generic template to chart series. My Template File: <Chart BackColor=\"211, 223, 240\" Width=\"250\" Height=\"100\" BackGradientStyle=\"TopBottom\" BackSecondaryColor=\"Blue\

The Mission: Apply a generic template to chart series.

My Template File:

<Chart BackColor="211, 223, 240" Width="250" Height="100" BackGradientStyle="TopBottom" BackSecondaryColor="Blue" BorderColor="26, 59, 105" BorderWidth="2">
  <Series>
    <series _Template_="All" Color="Black" BorderColor="180, 26, 59, 105">
    </series>
  </Series>
</Chart>

The styles applied to "Chart" work just fine. However, nothing applied to the Series template works; it merely contains the default styling.

My function that returns the chart image:

    private FileContentResult ImageCall(string config)
    {
        System.Web.UI.DataVisualization.Charting.Chart chart = new System.Web.UI.DataVisualization.Charting.Chart();


        if (config.Length > 0)
        {
            chart.Serializer.IsTemplateMode = true;
            chart.Serializer.IsResetWhenLoading = false;
            chart.Serializer.SerializableContent = "*.*";
            chart.Serializer.Load(config);
        }

        Series s2 = new Series("Series1");
        s2.ChartArea = "Area1";
        s2.ChartType = SeriesChartType.Column;
        s2.Points.Add(new DataPoint
        {
            AxisLabel = "Value1",
            YValues = new double[] { 1 }
        });
        s2.Points.Add(new DataPoint
        {
            AxisLabel = "Value2",
            YValues = new double[] { 2 }
        });
        chart.Series.Add(s2);

        ChartArea ca1 = new ChartArea("Area1");
        chart.ChartAreas.Add(ca1);

        using (var ms = new MemoryStream())
        {
            chart.SaveImage(ms, ChartImageFormat.Png);
            ms.Seek(0, SeekOrigin.Begin);

            return File(ms.ToArray(), "image/png", "mychart.png");
        }
    }

Here's what it looks like (crappy, I know... it's merely illustrative):

.NET Charting - Series Template

Note that the bars are unstyled while the chart seems to style just fine.

The documentat开发者_C百科ion regarding built-in .NET charting is woefully thin. Can anyone help me get my generic templates to work? There's a big green check mark in it for you!

I'm probably missing something obvious. Thanks!


Sigh

So the fix, like I thought, was simple. Let the world know that templates can only be applied to object on the chart after they are added to the chart. You cannot pre-set the styles then add the series, you need to add the series then set the style.

Here's the updated code:

private FileContentResult ImageCall(string config)
{
    System.Web.UI.DataVisualization.Charting.Chart chart = new System.Web.UI.DataVisualization.Charting.Chart();



    Series s2 = new Series("Series1");
    s2.ChartArea = "Area1";
    s2.ChartType = SeriesChartType.Column;
    s2.Points.Add(new DataPoint
    {
        AxisLabel = "Value1",
        YValues = new double[] { 1 }
    });
    s2.Points.Add(new DataPoint
    {
        AxisLabel = "Value2",
        YValues = new double[] { 2 }
    });
    chart.Series.Add(s2);

    ChartArea ca1 = new ChartArea("Area1");
    chart.ChartAreas.Add(ca1);

            //REPOSITIONED
    if (config.Length > 0)
    {
        chart.Serializer.IsTemplateMode = true;
        chart.Serializer.IsResetWhenLoading = false;
        chart.Serializer.SerializableContent = "*.*";
        chart.Serializer.Load(config);
    }

    using (var ms = new MemoryStream())
    {
        chart.SaveImage(ms, ChartImageFormat.Png);
        ms.Seek(0, SeekOrigin.Begin);

        return File(ms.ToArray(), "image/png", "mychart.png");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消