Exemplo n.º 1
0
        public void addSeries(string chartName, double[,] data, string dataName, string infoChart = "", string infoData = "")
        {
            // Finding the chart
            ExcelDrawing objChart = null;
            int          nCharts  = charts_.Count;

            for (int i = 0; i != nCharts; ++i)
            {
                ExcelDrawing chart = charts_[i];
                if (chart.Name == chartName)
                {
                    objChart   = chart;
                    lastSheet_ = chartSheets_[i];
                    break;
                }
            }
            if (objChart == null)
            {
                return;
            }
            // Check if the chart is currently in use
            ChartPosition pos;
            int           iRow, jCol;

            if (!posCharts_.ContainsKey(objChart))
            {
                pos = new ChartPosition()
                {
                    header = new Position {
                        row = ChartPosition.lastRow + 1, col = 1
                    },
                    length            = data.GetLength(0),
                    availablePosition = new Position {
                        row = ChartPosition.lastRow + 3, col = 1
                    }
                };
                // Write the header
                workSheet_.Cells[pos.header.row, pos.header.col].Value = objChart.Name + infoChart;
                posCharts_.Add(objChart, pos);
                ChartPosition.lastRow += pos.length + 3;
            }
            else
            {
                pos = posCharts_[objChart];
            }
            // Add the function values
            iRow = pos.availablePosition.row;
            jCol = pos.availablePosition.col;
            int nData = data.GetLength(0);

            for (int k = 0; k != nData; ++k)
            {
                workSheet_.Cells[iRow + k, jCol].Value     = data[k, 0];
                workSheet_.Cells[iRow + k, jCol + 1].Value = data[k, 1];
            }
            workSheet_.Cells[pos.header.row + 1, jCol].Value     = infoData; // Set the data info
            workSheet_.Cells[pos.header.row + 1, jCol + 1].Value = dataName; // Set the name
            // Retrieving the data address
            ExcelScatterChart scatterChart = (ExcelScatterChart)objChart;
            string            xVals        = ExcelRange.GetAddress(iRow, jCol,
                                                                   iRow + nData - 1, jCol);
            string yVals = ExcelRange.GetAddress(iRow, jCol + 1,
                                                 iRow + nData - 1, jCol + 1);

            xVals = ExcelRange.GetFullAddress(workSheetName_, xVals);
            yVals = ExcelRange.GetFullAddress(workSheetName_, yVals);
            // Creating the serie
            ExcelScatterChartSerie serie = scatterChart.Series.Add(yVals, xVals);
            // Using the standard markers when custom ones are not available
            List <MarkerProperty> markers = customMarkers_[chartName];

            if (markers == null || markers.Count == 0)
            {
                markers = standardMarkers_;
            }
            MarkerProperty markerProperties = markers[indMarkers_[chartName]];
            // Using the standard lines when custom ones are not available
            List <LineProperty> lines = customLines_[chartName];

            if (lines == null || lines.Count == 0)
            {
                lines = standardLines_;
            }
            LineProperty lineProperties = lines[indLines_[chartName]];
            int          transparency   = lineProperties.isTransparent ? 100 : 0; // Perecentage

            // Specifying the properties
            serie.Border.Fill.Color        = Color.Black;              // Line color
            serie.Border.LineStyle         = lineProperties.lineStyle; // Line style
            serie.Border.Fill.Transparancy = transparency;             // Line transparency
            serie.Border.Width             = 1.0;                      // Line width
            if (serie.Marker != null)
            {
                serie.Marker.Border.Fill.Color = Color.Black;   // Marker border color
                serie.Marker.Border.Width      = 0.75;          // Marker border width
                serie.Marker.Size = 5;                          // Marker size
                // Marker fill color
                if (markerProperties.fillColor != Color.Transparent)
                {
                    serie.Marker.Fill.Color = markerProperties.fillColor;
                }
                else
                {
                    serie.Marker.Fill.Style = eFillStyle.NoFill;
                }
                // Marker style
                if (lineProperties.isMarkersEnabled)
                {
                    serie.Marker.Style = markerProperties.style;
                }
                else
                {
                    serie.Marker.Style = eMarkerStyle.None;
                }
                // Increment markers and lines indices
                ++indMarkers_[chartName];
                if (indMarkers_[chartName] >= markers.Count)
                {
                    indMarkers_[chartName] = 0;
                }
            }
            ++indLines_[chartName];
            if (indLines_[chartName] >= lines.Count)
            {
                indLines_[chartName] = 0;
            }
            // Legend
            serie.Header = dataName;
            // Shifting data locations
            pos.availablePosition.col = pos.availablePosition.col + 2;
            pos.length = Math.Max(pos.length, nData);
            int lastRowColumn = pos.availablePosition.row + pos.length;

            if (lastRowColumn > ChartPosition.lastRow)
            {
                ChartPosition.lastRow = lastRowColumn;
            }
        }
Exemplo n.º 2
0
        public void createMarkers()
        {
            // Standard markers
            standardMarkers_ = new List <MarkerProperty>();
            standardMarkers_.Add(new MarkerProperty {
                fillColor = Color.White, style = eMarkerStyle.Square
            });
            standardMarkers_.Add(new MarkerProperty {
                fillColor = Color.Black, style = eMarkerStyle.Square
            });
            standardMarkers_.Add(new MarkerProperty {
                fillColor = Color.White, style = eMarkerStyle.Circle
            });
            standardMarkers_.Add(new MarkerProperty {
                fillColor = Color.Black, style = eMarkerStyle.Circle
            });
            standardMarkers_.Add(new MarkerProperty {
                fillColor = Color.White, style = eMarkerStyle.Triangle
            });
            standardMarkers_.Add(new MarkerProperty {
                fillColor = Color.Black, style = eMarkerStyle.Triangle
            });
            standardMarkers_.Add(new MarkerProperty {
                fillColor = Color.White, style = eMarkerStyle.Diamond
            });
            standardMarkers_.Add(new MarkerProperty {
                fillColor = Color.Black, style = eMarkerStyle.Diamond
            });
            standardMarkers_.Add(new MarkerProperty {
                fillColor = Color.Transparent, style = eMarkerStyle.X
            });
            // Check if the sequence is defined in the template file
            List <string> chartNames = getChartNames();

            foreach (string chart in chartNames)
            {
                customMarkers_.Add(chart, null);
            }
            ExcelWorksheet markersSheet = null;

            foreach (ExcelWorksheet sheet in package_.Workbook.Worksheets)
            {
                if (sheet.Name == markersSheetName_)
                {
                    markersSheet = sheet;
                }
            }
            // Reading the defined sequence of markers
            if (markersSheet == null)
            {
                return;
            }
            int nColumns = markersSheet.Dimension.Columns;
            int nRows    = markersSheet.Dimension.Rows;

            for (int iColumn = 1; iColumn <= nColumns; ++iColumn)
            {
                string chart = markersSheet.Cells[1, iColumn].Text;
                if (String.IsNullOrEmpty(chart))
                {
                    break;
                }
                if (!customMarkers_.ContainsKey(chart))
                {
                    continue;
                }
                List <MarkerProperty> markerProperties = new List <MarkerProperty>();
                for (int iRow = 2; iRow <= nRows; ++iRow)
                {
                    string         description = markersSheet.Cells[iRow, iColumn].Text.ToLower();
                    MarkerProperty marker      = new MarkerProperty();
                    marker.style     = eMarkerStyle.None;
                    marker.fillColor = Color.White;
                    switch (description)
                    {
                    case "□":
                        marker.style = eMarkerStyle.Square;
                        break;

                    case "■":
                        marker.style     = eMarkerStyle.Square;
                        marker.fillColor = Color.Black;
                        break;

                    case "○":
                        marker.style = eMarkerStyle.Circle;
                        break;

                    case "●":
                        marker.style     = eMarkerStyle.Circle;
                        marker.fillColor = Color.Black;
                        break;

                    case "△":
                        marker.style = eMarkerStyle.Triangle;
                        break;

                    case "▲":
                        marker.style     = eMarkerStyle.Triangle;
                        marker.fillColor = Color.Black;
                        break;

                    case "◇":
                        marker.style = eMarkerStyle.Diamond;
                        break;

                    case "◆":
                        marker.style     = eMarkerStyle.Diamond;
                        marker.fillColor = Color.Black;
                        break;

                    case "x":
                        marker.style     = eMarkerStyle.X;
                        marker.fillColor = Color.Transparent;
                        break;
                    }
                    // If a marker fits the set of the predefined words
                    if (marker.style != eMarkerStyle.None)
                    {
                        markerProperties.Add(marker);
                    }
                    else
                    {
                        break;
                    }
                }
                customMarkers_[chart] = markerProperties;
            }
        }