Пример #1
0
        public override void DrawChart(Series nseries, MetaFile ametafile, int posx, int posy, object achart)
        {
            System.Windows.Forms.DataVisualization.Charting.Chart nchart = new System.Windows.Forms.DataVisualization.Charting.Chart();

            float afontsize = Convert.ToSingle(Math.Round(nseries.FontSize * nseries.Resolution / 100));

            //achart.BackColor:=clTeeColor;
            nchart.BackColor = Color.White;

            /*achart.LeftAxis.LabelsFont.Name:=nchart.WFontName;
             * achart.BottomAxis.LabelsFont.Name:=nchart.WFontName;
             * achart.Legend.Font.Name:=nchart.WFontName;
             * achart.LeftAxis.LabelsFont.Style:=CLXIntegerToFontStyle(nchart.FontStyle);
             * achart.BottomAxis.LabelsFont.Style:=CLXIntegerToFontStyle(nchart.FontStyle);
             * achart.Legend.Font.Size:=aFontSize;
             * achart.Legend.Font.Style:=CLXIntegerToFontStyle(nchart.FontStyle);*/
            /*achart.Legend.Visible:=nchart.ShowLegend;*/
            // autorange and other ranges

            /*achart.LeftAxis.Maximum:=Series.HighValue;
             * achart.LeftAxis.Minimum:=Series.LowValue;
             * achart.LeftAxis.Automatic:=false;
             * achart.LeftAxis.AutomaticMaximum:=Series.AutoRangeH;
             * achart.LeftAxis.AutomaticMinimum:=Series.AutoRangeL;
             * achart.LeftAxis.LabelsAngle:=nchart.VertFontRotation mod 360;
             * achart.LeftAxis.LabelsFont.Size:=Round(nchart.VertFontSize*nchart.Resolution/100);
             * achart.BottomAxis.LabelsAngle:=nchart.HorzFontRotation mod 360;
             * achart.BottomAxis.LabelsFont.Size:=Round(nchart.HorzFontSize*nchart.Resolution/100);*/
            nchart.ChartAreas.Add("");
            if (nseries.Effect3D)
            {
                nchart.ChartAreas[0].Area3DStyle.Enable3D    = nseries.Effect3D;;
                nchart.ChartAreas[0].Area3DStyle.IsClustered = true;
            }
            //nchart.ChartAreas[0].AxisX.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
            //nchart.ChartAreas[0].AxisY.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
            //nchart.ChartAreas[0].AxisY.MinorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
            //nchart.ChartAreas[0].AxisX.MinorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
            nchart.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
            nchart.ChartAreas[0].AxisX.MajorGrid.LineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Dot;
            if (nseries.Logaritmic)
            {
                nchart.ChartAreas[0].AxisY.IsLogarithmic = true;
                nchart.ChartAreas[0].AxisY.LogarithmBase = nseries.LogBase;
            }
            nchart.ChartAreas[0].AxisY.IsReversed = nseries.Inverted;
            //nchart.ChartAreas[0].AxisX.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;
            nchart.ChartAreas[0].AxisX.IntervalAutoMode    = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.FixedCount;
            nchart.ChartAreas[0].AxisX.LabelStyle.Interval = 1;

            if (!nseries.AutoRangeH)
            {
                //nchart.ChartAreas[0].AxisX.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.
                nchart.ChartAreas[0].AxisY.Maximum = nseries.HighValue;
            }
            if (nseries.VertFontRotation != 0)
            {
                nchart.ChartAreas[0].AxisY.LabelStyle       = new System.Windows.Forms.DataVisualization.Charting.LabelStyle();
                nchart.ChartAreas[0].AxisY.LabelStyle.Angle = nseries.VertFontRotation;
            }
            if (nseries.HorzFontRotation != 0)
            {
                nchart.ChartAreas[0].AxisY.LabelStyle       = new System.Windows.Forms.DataVisualization.Charting.LabelStyle();
                nchart.ChartAreas[0].AxisY.LabelStyle.Angle = nseries.HorzFontRotation;
            }
            if (!nseries.AutoRangeL)
            {
                //nchart.ChartAreas[0].AxisX.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.
                nchart.ChartAreas[0].AxisY.Minimum = nseries.LowValue;
            }
            if (nseries.ShowLegend)
            {
                nchart.Legends.Add("");
            }
            // Create series
            int acolor   = 0;
            int idxserie = 0;

            foreach (SeriesItem sitem in nseries.SeriesItems)
            {
                System.Windows.Forms.DataVisualization.Charting.Series chartserie = nchart.Series.Add(sitem.Caption);
                switch (sitem.ChartStyle)
                {
                case ChartType.Area:
                    chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Area;
                    break;

                case ChartType.Gantt:
                case ChartType.Arrow:
                    chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                    break;

                case ChartType.Bar:
                    switch (nseries.MultiBar)
                    {
                    case BarType.Stacked:
                        chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn;
                        break;

                    case BarType.Stacked100:
                        chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedColumn100;
                        break;

                    default:
                        chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column;
                        break;
                    }
                    break;

                case ChartType.Bubble:
                    chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bubble;
                    break;

                case ChartType.Horzbar:
                    switch (nseries.MultiBar)
                    {
                    case BarType.Stacked:
                        chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar;
                        break;

                    case BarType.Stacked100:
                        chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StackedBar100;
                        break;

                    default:
                        chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Bar;
                        break;
                    }
                    break;

                case ChartType.Line:
                    chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                    break;

                case ChartType.Pie:
                    chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Pie;
                    break;

                case ChartType.Point:
                    chartserie.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Point;
                    break;
                }
                chartserie.Label = sitem.Caption;
                if (nseries.ShowHint)
                {
                    chartserie.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Square;
                }
                else
                {
                    chartserie.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.None;
                }
                if (sitem.ChartStyle == ChartType.Pie)
                {
                    // Set these other two properties so that you can see the connecting lines
                    nchart.Series[0].BorderWidth = 1;
                    nchart.Series[0].BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
                    // Set the pie label as well as legend text to be displayed as percentage
                    // The P2 indicates a precision of 2 decimals
                    //nchart.Series[0].Label = "#VALX #PERCENT{P2} #VAL";
                    nchart.Series[0].Label = "#PERCENT{P2}";

                    nchart.Series[0]["PieLabelStyle"] = "Outside";
                    //nchart.Legends[0].CustomItems.Add(npoint.Color, sitem.ValueCaptions[j]);
                    //nchart.Series[0].LegendText = "#VALX  (#PERCENT) #VAL";
                    nchart.Series[0].LegendText = "#VALX #VAL";
                    //this.Chart2.Series[0].LegendText = "#VALX (#PERCENT)"
                    //nchart.Legends[0].= sitem.ValueCaptions[j];

                    //n.Legends.Add("Legend1");
                    nchart.Legends[0].Enabled   = true;
                    nchart.Legends[0].Docking   = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;
                    nchart.Legends[0].Alignment = System.Drawing.StringAlignment.Center;
                }
                if (sitem.Color > 0)
                {
                    chartserie.Color = GraphicUtils.ColorFromInteger(sitem.Color);
                }
                else
                {
                    chartserie.Color = GraphicUtils.ColorFromInteger(SeriesColors[acolor]);
                }
                double total = 0;
                if (nseries.MarkStyle == 1)
                {
                    for (int j = 0; j < sitem.Values.Count; j++)
                    {
                        object nobj = sitem.Values[j];
                        if (DoubleUtil.IsNumericType(nobj))
                        {
                            total = total + Convert.ToDouble(nobj);
                        }
                    }
                }

                for (int j = 0; j < sitem.Values.Count; j++)
                {
                    string ncaption = sitem.ValueCaptions[j];
                    if (ncaption == null)
                    {
                        ncaption = "";
                    }
                    int idx = chartserie.Points.AddXY(ncaption, sitem.Values[j]);
                    System.Windows.Forms.DataVisualization.Charting.DataPoint npoint = chartserie.Points[idx];

                    if (sitem.ChartStyle != ChartType.Pie)
                    {
                        if (nseries.ShowHint)
                        {
                            switch (nseries.MarkStyle)
                            {
                            case 1:
                                npoint.Label = "";
                                if ((DoubleUtil.IsNumericType(sitem.Values[j])) && (total != 0))
                                {
                                    npoint.Label = (Convert.ToDouble(sitem.Values[j]) / total * 100).ToString("N2") + "%";
                                }
                                break;

                            case 6:
                                npoint.Label = "";
                                if (DoubleUtil.IsNumericType(sitem.Values[j]))
                                {
                                    double nuevo_valor = Convert.ToDouble(sitem.Values[j]);
                                    npoint.Label = nuevo_valor.ToString("N1") + "%";
                                }
                                break;

                            default:
                                npoint.Label = sitem.Values[j].ToString();
                                break;
                            }
                        }
                        else
                        {
                            npoint.Label = "";
                        }
                    }
                    if (nseries.SeriesItems.Count < 2)
                    {
                        if (sitem.Colors[j] >= 0)
                        {
                            npoint.Color = GraphicUtils.ColorFromInteger(sitem.Colors[j]);
                        }
                        else
                        {
                            npoint.Color = GraphicUtils.ColorFromInteger(SeriesColors[acolor]);
                        }
                        if ((sitem.ChartStyle == ChartType.Pie) || nseries.ShowLegend)
                        {
                            if (sitem.ChartStyle == ChartType.Pie)
                            {
                            }
                            else
                            {
                                if (j != 0)
                                {
                                    nchart.Legends[0].CustomItems.Add(npoint.Color, sitem.ValueCaptions[j]);
                                }
                                else
                                {
                                    nchart.Legends.Add(sitem.ValueCaptions[j]);
                                }
                            }

                            acolor = ((acolor + 1) % (MAX_SERIECOLORS));
                        }
                    }
                    else
                    {
                        if (sitem.Colors[j] >= 0)
                        {
                            npoint.Color = GraphicUtils.ColorFromInteger(sitem.Colors[j]);
                        }
                        else
                        {
                            npoint.Color = GraphicUtils.ColorFromInteger(SeriesColors[acolor]);
                        }
                    }
                }
                acolor = ((acolor + 1) % (MAX_SERIECOLORS));
                idxserie++;
            }

            Bitmap nbitmap = new Bitmap(Convert.ToInt32(Convert.ToSingle(Twips.TwipsToInch(nseries.PrintWidth)) * nseries.Resolution),
                                        Convert.ToInt32(Convert.ToSingle(Twips.TwipsToInch(nseries.PrintHeight)) * nseries.Resolution), PixelFormat.Format24bppRgb);
            Rectangle rec = new Rectangle(0, 0, nbitmap.Width, nbitmap.Height);

            nchart.SetBounds(0, 0, nbitmap.Width, nbitmap.Height);
            nchart.Update();
            nchart.DrawToBitmap(nbitmap, rec);
            System.IO.MemoryStream mstream = new MemoryStream();

            nbitmap.Save(mstream, ImageFormat.Bmp);

            MetaObjectImage metaobj = new MetaObjectImage();

            metaobj.MetaType       = MetaObjectType.Image;
            metaobj.Top            = posy; metaobj.Left = posx;
            metaobj.Width          = nseries.PrintWidth;
            metaobj.Height         = nseries.PrintHeight;
            metaobj.CopyMode       = 20;
            metaobj.DrawImageStyle = ImageDrawStyleType.Stretch;
            metaobj.DPIRes         = Convert.ToInt32(nseries.Resolution);
            metaobj.PreviewOnly    = false;
            metaobj.StreamPos      = ametafile.Pages[ametafile.CurrentPage].AddStream(mstream, false);
            metaobj.SharedImage    = false;
            metaobj.StreamSize     = mstream.Length;
            ametafile.Pages[ametafile.CurrentPage].Objects.Add(metaobj);

            base.DrawChart(nseries, ametafile, posx, posy, achart);
        }
Пример #2
0
        /// <summary>
        /// Draw RGB histogram of an image (each color as spline area)
        /// </summary>
        /// <param name="image">image</param>
        /// <returns>image of the RGB histogram</returns>
        public static Bitmap DrawRgbHistogramChart(Image image)
        {
            //based on https://code.msdn.microsoft.com/Professional-Image-b88e8871#content

            int width  = 256;
            int height = 128;

            var hist_img = new Bitmap(width, height);

            var chart = new System.Windows.Forms.DataVisualization.Charting.Chart();

            //chart.Dock = DockStyle.Fill;

            chart.AntiAliasing = System.Windows.Forms.DataVisualization.Charting.AntiAliasingStyles.All;

            chart.Size      = new Size(width, height);
            chart.BackColor = Color.LightGray;
            chart.ChartAreas.Add("Default");

            chart.Series.Add("Red");
            chart.Series.Add("Green");
            chart.Series.Add("Blue");

            // Set SplineArea chart type
            chart.Series["Red"].ChartType   = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.SplineArea;
            chart.Series["Green"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.SplineArea;
            chart.Series["Blue"].ChartType  = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.SplineArea;

            // set line tension
            chart.Series["Red"]["LineTension"]   = "0.8";
            chart.Series["Green"]["LineTension"] = "0.8";
            chart.Series["Blue"]["LineTension"]  = "0.8";

            // Set colour and transparency
            chart.Series["Red"].Color   = Color.FromArgb(200, Color.DarkRed);
            chart.Series["Green"].Color = Color.FromArgb(150, Color.DarkGreen);
            chart.Series["Blue"].Color  = Color.FromArgb(100, Color.DarkBlue);

            // Disable X & Y axis labels
            chart.ChartAreas["Default"].AxisX.LabelStyle.Enabled = false;
            chart.ChartAreas["Default"].AxisY.LabelStyle.Enabled = false;
            chart.ChartAreas["Default"].AxisX.MinorGrid.Enabled  = false;
            chart.ChartAreas["Default"].AxisX.MajorGrid.Enabled  = false;
            chart.ChartAreas["Default"].AxisY.MinorGrid.Enabled  = false;
            chart.ChartAreas["Default"].AxisY.MajorGrid.Enabled  = false;

            // load image to smaller size to speedup calculation
            var image_bmp = (Bitmap)CommonUtils.ImageUtils.Resize(image, smaller_size, smaller_size, true);

            var redHistogram   = GetHistogram(image_bmp, 0);
            var greenHistogram = GetHistogram(image_bmp, 1);
            var blueHistogram  = GetHistogram(image_bmp, 2);

            // Draw histogram

            chart.Series["Red"].Points.Clear();
            chart.Series["Green"].Points.Clear();
            chart.Series["Blue"].Points.Clear();

            float fudge_factor = 0.6f; // added to R/G to make them visible when all line overlap

            for (int i = 0; i < redHistogram.Length; i++)
            {
                chart.Series["Red"].Points.AddY(redHistogram[i] + fudge_factor);
                chart.Series["Green"].Points.AddY(greenHistogram[i] + 2 * fudge_factor);
                chart.Series["Blue"].Points.AddY(blueHistogram[i]);
            }
            chart.DrawToBitmap(hist_img, new Rectangle(0, 0, hist_img.Width - 1, hist_img.Height - 1));

            return(hist_img);
        }