Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem in time to let the dynamic chart.ChartAreas [0]. AxisY the y-axis to get the maximum value and minimum value, so you do not cut the spline chart

below is my code:

C#
private string GetSparkLineImage (IEnumerable <float> samples, int width, int height, int id,                SPWeb web // this because im using impersonate user )
        {
            var chart = new Chart ();
            var series = new Series ();
            chart.Series.Add (series);
 / / Sparklines use the 'Spline' chart type to show a smoother trend with a line chart
            series.ChartType = SeriesChartType.Spline;
            / / Since the line is the only thing you see on the chart, you might want to
            / / Increase its width. Interestingly, you need to set the BorderWidth property
            / / In order to ACCOMPLISH that.
            series.BorderWidth = 2;
            / / Add samples to the series
            int i = 0;
            DateTime start = new DateTime (2010, 1, 1);
            foreach (var sample in samples)
            {
                i + +;
               
                / / Add 5 to the sample so line does not get cut off at the bottom of the image

                Chart.Series [0]. Points.AddXY (start.AddYears (i) sample);
                / / Chart.Series [0]. Points.AddXY (i, sample);
            }


            / / Start hiding Both sets of axes, labels, gridlines and tick marks
            var = new ChartArea ChartArea ();
            chart.ChartAreas.Add (ChartArea);
            chartArea.AxisX.LabelStyle.Enabled = false;
            chartArea.AxisY.LabelStyle.Enabled = false;
            chartArea.AxisX.MajorGrid.Enabled = false;
            chartArea.AxisY.MajorGrid.Enabled = false;
            chartArea.AxisX.MajorTickMark.Enabled = false;
            chartArea.AxisY.MajorTickMark.Enabled = false;
            chartArea.AxisX.LineWidth = 0;
            chartArea.AxisY.LineWidth = 0;

            / / Code Pilot
            chart.ChartAreas [0]. AxisX.Minimum = 0;
            chart.ChartAreas [0]. AxisX.Maximum = 20;

            Chart.Series [0]. IsXValueIndexed = true;
            Chart.Series [0]. YValuesPerPoint = 4;

           

            min = int (int) chart.ChartAreas [0]. AxisX.Minimum;
            int max = (int) chart.ChartAreas [0]. AxisX.Maximum;

            if (max> Chart.Series [0]. Points.Count)
                Chart.Series = max [0]. Points.Count;

        

            var points Chart.Series = [0]. Points.Skip (min). Take (max - min);

            var minValue = points.Min (x => x.YValues ​​[0]);
            var maxValue = points.Max (x => x.YValues ​​[0] * 2);



            chart.ChartAreas [0]. AxisY.Minimum = minValue;
            chart.ChartAreas [0]. AxisY.Maximum = maxValue;


            / / End Code Pilot
            / / Add an extra 5 pixels since the samples were adjusted up by 5
            / / This Prevents the line from being truncated

            / / ChartArea.AxisY.Maximum maxValue = + 900;
            / / = ChartArea.AxisY.Minimum minValue - 100;

            / / Re-adjust the size of the chart to reduce unnecessary white space
            chart.Width = width;
            chart.Height = height;

            / / Get bytes of PNG image
            var s = new MemoryStream ();
            chart.SaveImage (s, ChartImageFormat.Png);
            s.Position = 0;

            Web.Lists SPList list = ["MyLibrary"];
            SPFolder folder = list.RootFolder;
            web.AllowUnsafeUpdates = true;
            byte [] picFile s.ToArray = ();
            var fname = folder.Files.Add ("img" + id + ". png", picFile, true);

            s.Position = 0;
            arq.Url return;

          }
Posted
Updated 21-Jun-13 4:41am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900