Setting a Series color

Hi,


I am trying to set a Series color :

Series s1 = chart.NSeries[s1_idx];
s1.Area.ForegroundColor = Color.Red;
or like this :
   for (int i = 0; i < s1.Points.Count; i++)
{
s1.Points[i].Area.ForegroundColor = Color.Red;
}

but nothing works, it automatically set a different color for it,
Please advice!
Thanks,
Udi

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

Please see the following code, it makes the series color to Red. You need to specify the formatting as FormattingType.Custom to make it work.

C#


Series srs= chart.NSeries[1];


srs.Area.Formatting = FormattingType.Custom;

srs.Area.ForegroundColor = Color.Red;


Thanks for the quick reply, this is not working for me, here is the full code , please advice :


 Workbook book1 = new Aspose.Cells.Workbook();
book1.FileFormat = FileFormatType.Xlsx;
        <span style="color:#2b91af;">Worksheet</span> sheet1 = book1.Worksheets[0];

        sheet1.Cells[0,0].Value = <span style="color:#a31515;">"X"</span>;
        sheet1.Cells[0,1].Value = <span style="color:#a31515;">"Y"</span>;

        <span style="color:#2b91af;">Random</span> R = <span style="color:blue;">new</span> <span style="color:#2b91af;">Random</span>();

        <span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 1; i < 21; i++)
        {
            sheet1.Cells[i, 0].Value = i;
            sheet1.Cells[i, 1].Value = <span style="color:#2b91af;">Math</span>.Round(R.NextDouble(), 1);
        }

        <span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 21; i < 41; i++)
        {
            sheet1.Cells[i, 0].Value = i-20;
            sheet1.Cells[i, 1].Value = 0.8;<span style="color:green;">// Math.Round(R.NextDouble(), 1);</span>
        }


        <span style="color:blue;">int</span> idx = sheet1.Charts.Add(<span style="color:#2b91af;">ChartType</span>.LineWithDataMarkers, 1, 3, 20, 20);            
        <span style="color:#2b91af;">Chart</span> chart = sheet1.Charts[idx];
        chart.Style = 3;
        chart.AutoScaling = <span style="color:blue;">true</span>;
        chart.PlotArea.Area.ForegroundColor = <span style="color:#2b91af;">Color</span>.White;
        <span style="color:green;">//chart.NSeries.IsColorVaried = true;</span>

        <span style="color:green;">//Set Properties of chart title </span>
        chart.Title.Text = <span style="color:#a31515;">"PCS"</span>;
        chart.Type = <span style="color:#2b91af;">ChartType</span>.Scatter;

        <span style="color:green;">//Set Properties of categoryaxis title </span>
        chart.CategoryAxis.Title.Text = <span style="color:#a31515;">"Units"</span>;

        <span style="color:green;">//Set Properties of nseries</span>
        <span style="color:blue;">int</span> s1_idx = chart.NSeries.Add(<span style="color:#a31515;">"A1:A1"</span>, <span style="color:blue;">true</span>);
        <span style="color:blue;">int</span> s2_idx = chart.NSeries.Add(<span style="color:#a31515;">"A22:A22"</span>, <span style="color:blue;">true</span>);

        chart.NSeries.IsColorVaried = <span style="color:blue;">true</span>;
        

        <span style="color:#2b91af;">Series</span> s1 = chart.NSeries[s1_idx];         
        s1.Area.Formatting = <span style="color:#2b91af;">FormattingType</span>.Custom;
        s1.Area.ForegroundColor = <span style="color:#2b91af;">Color</span>.Red;


        s1.XValues = <span style="color:#a31515;">"A2:A21"</span>;
        s1.Values = <span style="color:#a31515;">"B2:B21"</span>;

        <span style="color:#2b91af;">Series</span> s2 = chart.NSeries[s2_idx];
        s2.Area.Formatting = <span style="color:#2b91af;">FormattingType</span>.Custom;
        s2.Area.ForegroundColor = <span style="color:#2b91af;">Color</span>.Yellow;

        s2.XValues = <span style="color:#a31515;">"A22:A41"</span>;
        s2.Values = <span style="color:#a31515;">"B22:B41"</span>;
        
        <span style="color:green;">//s2.Type = ChartType.Line;           </span>
        
        
        chart.ToImage(<span style="color:#a31515;">@"C:\temp\chart1.gif"</span>);
        
        book1.Save(<span style="color:#a31515;">@"c:\temp\excel3.xls"</span>);
        <span style="color:#2b91af;">Process</span>.Start(<span style="color:#a31515;">@"c:\temp\excel3.xls"</span>);</pre></div>

Hi,

Thanks for your posting and using Aspose.Cells.

You need to use the following code to make it work. Please use the Marker property and set its foreground color.

C#


s1.Marker.Area.ForegroundColor = Color.Yellow;

Thanks a lot, this works!

but now if i try to add a new series with different Chart Type (Line) it does not change the color :

            Series s3 = chart.NSeries[s3_idx];
s3.Name = “GB”;
s3.Type = ChartType.Line;
s3.Area.Formatting = FormattingType.Custom;
s3.Area.ForegroundColor = Color.Green;


Thanks,
Udi

Hi,


Could you try to change the line, i.e…,
 s3.Area.ForegroundColor = Color.Green;     
to:
 s3.Marker.Area.ForegroundColor = Color.Green;   
if it works fine.

Thank you.  

Hi,

No - it doesn’t work.

Udi

Hi,


Here is the sample code (runnable) that I am using and it works fine. The third series is added with green color.

Sample code:

Workbook book1 = new Aspose.Cells.Workbook();
book1.FileFormat = FileFormatType.Xlsx;

Worksheet sheet1 = book1.Worksheets[0];

sheet1.Cells[0, 0].Value = “X”;
sheet1.Cells[0, 1].Value = “Y”;

Random R = new Random();

for (int i = 1; i < 21; i++)
{
sheet1.Cells[i, 0].Value = i;
sheet1.Cells[i, 1].Value = Math.Round(R.NextDouble(), 1);
}

for (int i = 21; i < 41; i++)
{
sheet1.Cells[i, 0].Value = i - 20;
sheet1.Cells[i, 1].Value = 0.8;// Math.Round(R.NextDouble(), 1);
}


for (int i = 41; i < 61; i++)
{
sheet1.Cells[i, 0].Value = i - 40;
sheet1.Cells[i, 1].Value = 0.9;// Math.Round(R.NextDouble(), 1);
}


int idx = sheet1.Charts.Add(ChartType.LineWithDataMarkers, 1, 3, 20, 20);
Chart chart = sheet1.Charts[idx];
chart.Style = 3;
chart.AutoScaling = true;
chart.PlotArea.Area.ForegroundColor = Color.White;
// chart.NSeries.IsColorVaried = true;

//Set Properties of chart title
chart.Title.Text = “PCS”;
chart.Type = ChartType.Scatter;

//Set Properties of categoryaxis title
chart.CategoryAxis.Title.Text = “Units”;

//Set Properties of nseries
int s1_idx = chart.NSeries.Add(“A1:A1”, true);
int s2_idx = chart.NSeries.Add(“A22:A22”, true);
int s3_idx = chart.NSeries.Add(“A42:A42”, true);

chart.NSeries.IsColorVaried = true;


Series s1 = chart.NSeries[s1_idx];
s1.Area.Formatting = FormattingType.Custom;
s1.Marker.Area.ForegroundColor = Color.Red;


s1.XValues = “A2:A21”;
s1.Values = “B2:B21”;

Series s2 = chart.NSeries[s2_idx];
s2.Area.Formatting = FormattingType.Custom;
s2.Marker.Area.ForegroundColor = Color.Yellow;

s2.XValues = “A22:A41”;
s2.Values = “B22:B41”;


Series s3 = chart.NSeries[s3_idx];
s3.Area.Formatting = FormattingType.Custom;
s3.Marker.Area.ForegroundColor = Color.Green;

s3.XValues = “A42:A61”;
s3.Values = “B42:B61”;


chart.ToImage(@“e:\test2\chart1.gif”);

book1.Save(@“e:\test2\outfile.xls”);

If you still find the issue, could you paste your simple runnable code, so that we could check your issue.

Thank you.

Hi,

Thanks for your posting and using Aspose.Cells for .NET.

We have checked your issue and found that it works fine. Please use the series1.Marker.Area.ForegroundColor property for your needs.