Beispiel #1
0
        private void ZedGraphStyle_ZedGraphDrawing(object sender, ZedGraphDrawingEventArgs e)
        {
            ZedGraphControl zedGraph = new ZedGraphControl();

            zedGraph.Size = new Size(100, 100);

            zedGraph.GraphPane.Fill.Type       = FillType.None;
            zedGraph.GraphPane.Chart.Fill.Type = FillType.None;

            zedGraph.GraphPane.Border.IsVisible       = false;
            zedGraph.GraphPane.Chart.Border.IsVisible = false;
            zedGraph.GraphPane.XAxis.IsVisible        = false;
            zedGraph.GraphPane.YAxis.IsVisible        = false;
            zedGraph.GraphPane.Legend.IsVisible       = false;
            zedGraph.GraphPane.Title.IsVisible        = false;

            for (int i = 0; i < SelectedColumns.Count; i++)
            {
                double value = 0;
                if (!double.TryParse(e.Feature.ColumnValues[SelectedColumns[i]], out value))
                {
                    zedGraph.Dispose();
                    return;
                }
                Color   color   = System.Drawing.Color.FromArgb(pieColors[i].AlphaComponent, pieColors[i].RedComponent, pieColors[i].GreenComponent, pieColors[i].BlueComponent);
                PieItem pieItem = zedGraph.GraphPane.AddPieSlice(value, color, 0.08, "");
                pieItem.LabelDetail.IsVisible = false;
            }
            zedGraph.AxisChange();

            e.Bitmap = zedGraph.GraphPane.GetImage();
            zedGraph.Dispose();
        }
Beispiel #2
0
 private void AssignGraph(ref ZedGraphControl graphControl, ZedGraphControl newGraphControl, Panel parentPanel)
 {
     if (graphControl != null)
     {
         parentPanel.Controls.Remove(graphControl);
         graphControl.Dispose();
     }
     graphControl = newGraphControl;
 }
Beispiel #3
0
        public static ZedGraphControl getGraphControl()
        {
            if (ct != null)
            {
                try
                {
                    ct.Dispose();
                    ct = null;
                }
                catch
                {
                    ct = null;
                }
            }

            int       n  = 5;
            Exception ex = null;

            while (n > 0)
            {
                try
                {
                    n--;
                    ct = new ZedGraphControl();
                    return(ct);

                    break;
                }
                catch (Exception exc)
                {
                    ex = exc;

                    if (ct != null)
                    {
                        ct = null;
                    }

                    System.Threading.Thread.Sleep(100);
                }
            }

            throw (ex);
        }
        public static string SaveDataToPng(double[] data, int id)
        {
            ZedGraphControl zedGraphControl1 = new ZedGraphControl();
            GraphPane       paneA            = new GraphPane();
            PointPairList   listA            = new PointPairList();

            paneA.Title.IsVisible = false;
            paneA.XAxis.IsVisible = false;
            paneA.YAxis.IsVisible = false;
            if (id != 0)
            {
                paneA.XAxis.Scale.Max = 300000;
            }
            else
            {
                paneA.XAxis.Scale.Max = 550000;
            }

            for (int i = 0; i < data.Length; i++)
            {
                listA.Add(i, data[i]);
            }
            LineItem myCurveA = paneA.AddCurve("", listA, Color.Blue, SymbolType.None);

            using (Graphics g = zedGraphControl1.CreateGraphics())
            {
                paneA.AxisChange(g);
                paneA.ReSize(g, new RectangleF(0, 0, 1800, 300));
            }

            string url      = @"D:\\PipeWeb\\AdImages\\";
            string filename = DateTime.Now.ToString("yyyy-MM-dd") + "--" + DateTime.Now.Hour.ToString() + "-" + DateTime.Now.Minute.ToString() + "-" + DateTime.Now.Second.ToString() + "--" + id.ToString();//以日期时间命名,避免文件名重复
            string strName  = url + filename + ".png";

            if (!Directory.Exists(url))         //如果不存在就创建file文件夹                 
            {
                Directory.CreateDirectory(url); //创建该文件夹 
                paneA.GetImage().Save(strName, ImageFormat.Png);
            }
            else
            {
                paneA.GetImage().Save(strName, ImageFormat.Png);
            }
            zedGraphControl1.Dispose();

            return(strName);
        }
Beispiel #5
0
        /// <summary>
        /// Creates an image of a barchart which represents the data in <paramref name="barChartData"/>.  The image is saved at <paramref name="completeFileName"/>.
        /// </summary>
        /// <param name="completeFileName">The path at which to save the generated image.</param>
        /// <param name="barChartData">The &lt;year, value&gt; plots for the barchart.</param>
        /// <returns>True on success, false on fail.</returns>
        private bool CreateBarChartImageFile(string completeFileName, SortedDictionary <int, double> barChartData)
        {
            if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
            {
                return(this.InvokeCreateBarChartImageFile(completeFileName, barChartData));
            }

            try
            {
                const string fontFamily = "Times New Roman";
                const float  fontSize   = 12f;


                ZedGraphControl chart = new ZedGraphControl();
                chart.Size = new Size((barChartData.Count + 2) * 45, 300);

                GraphPane pane = chart.GraphPane;
                pane.IsFontsScaled = false;

                pane.BarSettings.Type = BarType.Stack;
                pane.Chart.Fill       = new Fill(Color.Transparent);
                pane.Fill             = new Fill(Color.LightBlue, Color.AliceBlue, 90F);

                pane.Title.FontSpec.Family = fontFamily;
                pane.Title.FontSpec.Size   = fontSize;
                pane.Title.IsVisible       = false;
                pane.Title.Text            = string.Empty;

                pane.XAxis.MajorGrid.IsVisible = false;

                pane.XAxis.MajorTic.IsInside = false;

                pane.XAxis.Scale.FontSpec.Family = fontFamily;
                pane.XAxis.Scale.FontSpec.Size   = fontSize;

                pane.XAxis.Title.FontSpec.Family = fontFamily;
                pane.XAxis.Title.FontSpec.Size   = fontSize;
                pane.XAxis.Title.IsVisible       = false;
                pane.XAxis.Title.Text            = string.Empty;

                pane.X2Axis.IsVisible           = false;
                pane.X2Axis.MajorGrid.IsVisible = false;
                pane.X2Axis.MinorGrid.IsVisible = false;
                pane.X2Axis.MajorTic.IsInside   = false;
                pane.X2Axis.MajorTic.IsOutside  = false;
                pane.X2Axis.MinorTic.IsInside   = false;
                pane.X2Axis.MinorTic.IsOutside  = false;

                pane.YAxis.MajorGrid.IsVisible = true;
                pane.YAxis.MajorGrid.Color     = Color.LightSteelBlue;


                pane.YAxis.MajorTic.IsInside = false;

                pane.YAxis.MinorTic.IsInside  = false;
                pane.YAxis.MinorTic.IsOutside = false;


                pane.YAxis.Scale.FontSpec.Family = fontFamily;
                pane.YAxis.Scale.FontSpec.Size   = fontSize;

                pane.YAxis.Title.FontSpec.Family = fontFamily;
                pane.YAxis.Title.FontSpec.Size   = fontSize;
                pane.YAxis.Title.IsVisible       = false;
                pane.YAxis.Title.Text            = string.Empty;


                pane.Y2Axis.IsVisible           = false;
                pane.Y2Axis.MajorGrid.IsVisible = false;
                pane.Y2Axis.MinorGrid.IsVisible = false;
                pane.Y2Axis.MajorTic.IsInside   = false;
                pane.Y2Axis.MajorTic.IsOutside  = false;
                pane.Y2Axis.MinorTic.IsInside   = false;
                pane.Y2Axis.MinorTic.IsOutside  = false;

                int      idx    = 0;
                string[] labels = new string[barChartData.Count];

                PointPairList nPoints = new PointPairList();
                PointPairList pPoints = new PointPairList();
                foreach (KeyValuePair <int, double> yearValue in barChartData)
                {
                    int yr = yearValue.Key % 100;
                    labels[idx] = "'" + yr.ToString("00");

                    double value = yearValue.Value * 100;
                    if (yearValue.Value >= 0)
                    {
                        nPoints.Add(yearValue.Key, 0.0);
                        pPoints.Add(yearValue.Key, value);
                    }
                    else
                    {
                        nPoints.Add(yearValue.Key, value);
                        pPoints.Add(yearValue.Key, 0.0);
                    }

                    idx++;
                }

                pane.XAxis.Scale.TextLabels = labels;
                pane.XAxis.Type             = AxisType.Text;


                //negative values
                BarItem nBar = pane.AddBar(string.Empty, nPoints, Color.Empty);
                nBar.Bar.Fill = new Fill(Color.FromArgb(227, 99, 52), Color.FromArgb(255, 177, 126), Color.FromArgb(227, 99, 52));

                //positive values
                BarItem pBar = pane.AddBar(string.Empty, pPoints, Color.Empty);
                pBar.Bar.Fill = new Fill(Color.FromArgb(81, 148, 157), Color.FromArgb(107, 197, 222), Color.FromArgb(81, 148, 157));

                //1 - Update the chart with the series
                chart.AxisChange();

                //2 - Process the value labels for each point
                this.WriteValueLabels(pane, nBar, fontFamily, fontSize, true);
                this.WriteValueLabels(pane, pBar, fontFamily, fontSize, false);

                //3 - Update the chart again
                chart.AxisChange();

                using (Image img = chart.GetImage())
                {
                    EncoderParameters parms = new EncoderParameters(1);
                    EncoderParameter  parm  = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 80L);
                    parms.Param[0] = parm;

                    ImageCodecInfo jgpEncoder = GetEncoder(ImageFormat.Jpeg);
                    img.Save(completeFileName, jgpEncoder, parms);
                }

                try
                {
                    chart.Dispose();
                    chart = null;
                }
                catch { }

                return(true);
            }
            catch { }

            return(false);
        }