ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Making a transparent background on a .NET XYChart png
Posted by Matthew Nichols on Jan-18-2014 04:57
I have created a chart using your component and on the whole I find it to be a very nice tool.  There is one difficulty I am having.  I can't get my image to have a transparent background.  Here is the code I am running (a lot of this is not related but I wanted to be through so I included the entire image function)

public virtual byte[] GenerateAccountBalanceChart(DtoAccountBalanceChartData data)
{
var chart = new XYChart(Size.Width, Size.Height);
        //I have tried with the bellow line commented and uncommented
//chart.setTransparentColor(Chart.Transparent);
chart.setBackground(Chart.Transparent);

var backGroundColor = CalculateBackgroundColor(chart);
var plotArea = chart.setPlotArea(75, 15, 215, 150, backGroundColor, -1, Chart.Transparent);
plotArea.setGridColor(Chart.CColor(GridColor));

chart.setDefaultFonts(ChartFontFamily);

var intervalData = CalculateIntervalData(data.DataPoints);

var yAxis = chart.yAxis();
yAxis.setLabelFormat("{value|0,.}" + intervalData.LabelSuffix);
yAxis.setTickColor(Chart.CColor(YAxisTickColor));
yAxis.setLinearScale(0, intervalData.LargestLabelValue, intervalData.Interval);
yAxis.setLabelStyle(ChartFontFamily, FontSize, Chart.CColor(LabelColor));

var dataPoints = data.DataPoints.OrderBy(p => p.Date).ToList();

var xAxis = chart.xAxis();
var xAxxisLabelStyle = xAxis.setLabelStyle(ChartFontFamily, FontSize, Chart.CColor(LabelColor));
xAxxisLabelStyle.setFontAngle(90);

xAxis.setLabelFormat("{value|mmm dd}");
xAxis.setLabels(dataPoints.Select(dp => dp.Date).ToArray());
xAxis.setTickColor(Chart.CColor(YAxisTickColor));

var barLayer = chart.addBarLayer(
dataPoints.Select(dp => Convert.ToDouble(dp.Amount / intervalData.ValueDivisor)).ToArray(),
CalculateBarColor(chart), "Balance"
);
barLayer.setBorderColor(Chart.CColor(BorderColor));
barLayer.setBarWidth(BarWidth);

using (var ms = new MemoryStream())
{
var image = chart.makeImage();
image.Save(ms, ImageFormat.Png);
return ms.GetBuffer();
}
}

And then we use it this way, providing a name ending in png to the filename

var imageAsByteArray = generator.GenerateAccountBalanceChart(data);
var imagePath = Path.Combine("c:\\\\TestImages", fileName);
File.WriteAllBytes(imagePath, imageAsByteArray);

I am sure that I am missing a detail.  Any help is appreciated.

Matthew

  Re: Making a transparent background on a .NET XYChart png
Posted by Peter Kwan on Jan-21-2014 00:50
Hi Matthew,

If you intention is to create a PNG image in memory, instead of outputting as
System.Drawing.Image, then ask the System.Drawing.Image to save as PNG, you may
directly ask ChartDirector to output as PNG:

//
//using (var ms = new MemoryStream())
//{
//var image = chart.makeImage();
//image.Save(ms, ImageFormat.Png);
//return ms.GetBuffer();
//}
//

return chart.makeChart(Chart.PNG);

Hope this can help.

Regards
Peter Kwan