Esempio n. 1
0
        public void createLines()
        {
            // Standard lines
            standardLines_ = new List <LineProperty>();
            standardLines_.Add(new LineProperty {
                lineStyle = eLineStyle.Solid, isMarkersEnabled = true, isTransparent = false
            });
            List <string> chartNames = getChartNames();

            foreach (string chart in chartNames)
            {
                customLines_.Add(chart, null);
            }
            ExcelWorksheet linesSheet = null;
            ExcelWorkbook  book       = package_.Workbook;

            foreach (ExcelWorksheet sheet in book.Worksheets)
            {
                if (sheet.Name == linesSheetName_)
                {
                    linesSheet = sheet;
                    break;
                }
            }
            if (linesSheet == null)
            {
                return;
            }
            int nColumns = linesSheet.Dimension.Columns;
            int nRows    = linesSheet.Dimension.Rows;

            for (int iColumn = 1; iColumn <= nColumns; ++iColumn)
            {
                string chart = linesSheet.Cells[1, iColumn].Text;
                if (String.IsNullOrEmpty(chart))
                {
                    break;
                }
                if (!customLines_.ContainsKey(chart))
                {
                    continue;
                }
                List <LineProperty> lineProperties = new List <LineProperty>();
                for (int iRow = 2; iRow <= nRows; ++iRow)
                {
                    string       description = linesSheet.Cells[iRow, iColumn].Text.ToLower();
                    LineProperty line        = new LineProperty();
                    bool         isCorrect   = true;
                    switch (description)
                    {
                    case "━":
                        line.lineStyle        = eLineStyle.Solid;
                        line.isMarkersEnabled = false;
                        line.isTransparent    = false;
                        break;

                    case "--":
                        line.lineStyle        = eLineStyle.Dash;
                        line.isMarkersEnabled = false;
                        line.isTransparent    = false;
                        break;

                    case "x":
                        line.lineStyle        = eLineStyle.Solid;
                        line.isMarkersEnabled = true;
                        line.isTransparent    = true;
                        break;

                    case "x━":
                        line.lineStyle        = eLineStyle.Solid;
                        line.isMarkersEnabled = true;
                        line.isTransparent    = false;
                        break;

                    case "x--":
                        line.lineStyle        = eLineStyle.Dash;
                        line.isMarkersEnabled = true;
                        line.isTransparent    = false;
                        break;

                    default:
                        isCorrect = false;
                        break;
                    }
                    // If a marker fits the set of the predefined words
                    if (isCorrect)
                    {
                        lineProperties.Add(line);
                    }
                    else
                    {
                        break;
                    }
                }
                customLines_[chart] = lineProperties;
            }
        }
Esempio n. 2
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;
            }
        }