this is the sample code of dynamically created chart .
private void NewCostVaraianceChart()
{
try
{
if (taskdata != null)
{
tasklist = taskdata.ToList();
}
StackPanel stkpanel = new StackPanel();
int chartwidth = 400;
int taskcount = 3;
Style style = new Style(typeof(Control));
ResourceDictionaryCollection palette = new ResourceDictionaryCollection();
for (int i = 0; i < taskdata.Count; i++)
{
Chart barChart = new Chart();
barChart.Style = Application.Current.Resources["ChartStyleSch"] as Style;
palette = new ResourceDictionaryCollection();
barChart.Palette.Clear();
barChart.Axes.Clear();
barChart.Series.Clear();
if (i % taskcount == 0)
{
stkpanel = new StackPanel();
stkpanel.Orientation = Orientation.Horizontal;
}
CustomColumnSeries obj_columsrs = new CustomColumnSeries();
barChart.Height = 225;
barChart.Width = chartwidth;
barChart.Margin = new Thickness(10, 5, 10, 5);
obj_columsrs.IndependentValuePath = "ProjectName";
obj_columsrs.AnimationSequence = AnimationSequence.FirstToLast;
List<chartdata> cust = new List<chartdata>();
cust.Add(new ChartData()
{
WorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedHour),
ScheduledHr = Convert.ToInt32(tasklist[i].Scheduledhr),
ProjectName = tasklist[i].Projectname,
ColumnColors = new SolidColorBrush(GetRandomColor()),
tcolor = new SolidColorBrush(Colors.Blue),
ChangedHr = Convert.ToInt32(tasklist[i].Balance),
ReWorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedMin)
});
List<chartdata> cust1 = new List<chartdata>();
cust1.Add(new ChartData()
{
WorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedHour),
ScheduledHr = Convert.ToInt32(tasklist[i].Scheduledhr),
ProjectName = tasklist[i].Projectname,
tcolor = new SolidColorBrush(Colors.Blue),
ColumnColors = new SolidColorBrush(GetRandomColor()),
ChangedHr = Convert.ToInt32(tasklist[i].Balance),
ReWorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedMin)
});
List<chartdata> cust2 = new List<chartdata>();
cust2.Add(new ChartData()
{
WorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedHour),
ScheduledHr = Convert.ToInt32(tasklist[i].Scheduledhr),
ProjectName = tasklist[i].Projectname,
tcolor = new SolidColorBrush(Colors.Blue),
ColumnColors = new SolidColorBrush(GetRandomColor()),
ChangedHr = Convert.ToInt32(tasklist[i].Balance),
ReWorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedMin)
});
List<chartdata> cust3 = new List<chartdata>();
cust3.Add(new ChartData()
{
WorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedHour),
ScheduledHr = Convert.ToInt32(tasklist[i].Scheduledhr),
ProjectName = tasklist[i].Projectname,
tcolor = new SolidColorBrush(Colors.Blue),
ColumnColors = new SolidColorBrush(GetRandomColor()),
ChangedHr = Convert.ToInt32(tasklist[i].Balance),
ReWorkedHr = Convert.ToInt32(tasklist[i].TaskWorkedMin)
});
var getMxvalue = 0.0;
if (tasklist[i].Scheduledhr > tasklist[i].TaskWorkedHour && tasklist[i].Scheduledhr > tasklist[i].Balance && tasklist[i].Scheduledhr > tasklist[i].TaskWorkedMin)
{
getMxvalue = Convert.ToDouble(tasklist[i].Scheduledhr);
}
else if (tasklist[i].TaskWorkedHour > tasklist[i].Scheduledhr && tasklist[i].TaskWorkedHour > tasklist[i].Balance && tasklist[i].TaskWorkedHour > tasklist[i].TaskWorkedMin)
{
getMxvalue = Convert.ToDouble(tasklist[i].TaskWorkedHour);
}
else if (tasklist[i].Balance > tasklist[i].TaskWorkedHour && tasklist[i].Balance > tasklist[i].Scheduledhr && tasklist[i].Balance > tasklist[i].TaskWorkedMin)
{
getMxvalue = Convert.ToDouble(tasklist[i].Balance);
}
else if (tasklist[i].TaskWorkedMin > tasklist[i].TaskWorkedHour && tasklist[i].TaskWorkedMin > tasklist[i].Scheduledhr && tasklist[i].TaskWorkedMin > tasklist[i].Balance)
{
getMxvalue = Convert.ToDouble(tasklist[i].TaskWorkedMin);
}
var ValToRound = Math.Ceiling(Convert.ToDouble(getMxvalue) / 2);
if (ValToRound > 10)
{
ValToRound = RoundOff(((int)ValToRound));
}
LinearAxis linax = new LinearAxis();
linax.Orientation = AxisOrientation.Y;
barChart.Axes.Add(linax);
LinearAxis linax1 = barChart.Axes[0] as LinearAxis;
linax1.Minimum = 0;
linax1.Interval = ValToRound;
linax1.Maximum = Math.Ceiling(getMxvalue) + Math.Ceiling(ValToRound);
CategoryAxis catax1 = new CategoryAxis();
catax1.Orientation = AxisOrientation.Y;
barChart.Axes.Add(catax1);
CategoryAxis catax = barChart.Axes[1] as CategoryAxis;
catax.Title = ResourceFile.HoursLabel;
catax1.Location = AxisLocation.Left;
obj_columsrs.DependentValuePath = "ScheduledHr";
obj_columsrs.Title = " " + ResourceFile.ScheduledLabel;
barChart.Series.Add(obj_columsrs);
style = new Style(typeof(Control));
style.Setters.Add(new Setter(MaxWidthProperty, 30));
style.Setters.Add(new Setter(BackgroundProperty, cust[0].ColumnColors));
style.Setters.Add(new Setter(BorderBrushProperty, cust[0].ColumnColors));
style.Setters.Add(new Setter(TemplateProperty, SetColumnSeriesContentTemplate("'-45,0,25,0'")));
ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Add("DataPointStyle", style);
palette.Add(dictionary);
barChart.Palette = palette;
((CustomColumnSeries)barChart.Series[0]).ItemsSource = cust;
obj_columsrs = new CustomColumnSeries();
obj_columsrs.IndependentValuePath = "ProjectName";
obj_columsrs.AnimationSequence = AnimationSequence.FirstToLast;
obj_columsrs.DependentValuePath = "WorkedHr";
obj_columsrs.Title = obj_columsrs.Title = " " + ResourceFile.WorkedLabel;
style = new Style(typeof(Control));
style.Setters.Add(new Setter(MaxWidthProperty, 30));
style.Setters.Add(new Setter(BackgroundProperty, cust1[0].ColumnColors));
style.Setters.Add(new Setter(BorderBrushProperty, cust1[0].ColumnColors));
style.Setters.Add(new Setter(TemplateProperty, SetColumnSeriesContentTemplate("'-20,0,0,0'")));
dictionary = new ResourceDictionary();
dictionary.Add("DataPointStyle", style);
palette.Add(dictionary);
barChart.Palette = palette;
barChart.Series.Add(obj_columsrs);
((CustomColumnSeries)barChart.Series[1]).ItemsSource = cust1;
obj_columsrs = new CustomColumnSeries();
obj_columsrs.IndependentValuePath = "ProjectName";
obj_columsrs.AnimationSequence = AnimationSequence.FirstToLast;
obj_columsrs.DependentValuePath = "ChangedHr";
obj_columsrs.Title = obj_columsrs.Title = " " + ResourceFile.ChangeHour;
style = new Style(typeof(Control));
style.Setters.Add(new Setter(MaxWidthProperty, 30));
style.Setters.Add(new Setter(BackgroundProperty, cust2[0].ColumnColors));
style.Setters.Add(new Setter(BorderBrushProperty, cust2[0].ColumnColors));
style.Setters.Add(new Setter(TemplateProperty, SetColumnSeriesContentTemplate("'5,0,-25,0'")));
dictionary = new ResourceDictionary();
dictionary.Add("DataPointStyle", style);
palette.Add(dictionary);
barChart.Palette = palette;
barChart.Series.Add(obj_columsrs);
((CustomColumnSeries)barChart.Series[2]).ItemsSource = cust2;
obj_columsrs = new CustomColumnSeries();
obj_columsrs.IndependentValuePath = "ProjectName";
obj_columsrs.AnimationSequence = AnimationSequence.FirstToLast;
obj_columsrs.DependentValuePath = "ReWorkedHr";
obj_columsrs.Title = obj_columsrs.Title = " " + ResourceFile.ReworkHour;
style = new Style(typeof(Control));
style.Setters.Add(new Setter(MaxWidthProperty, 30));
style.Setters.Add(new Setter(BackgroundProperty, cust3[0].ColumnColors));
style.Setters.Add(new Setter(BorderBrushProperty, cust3[0].ColumnColors));
style.Setters.Add(new Setter(TemplateProperty, SetColumnSeriesContentTemplate("'30,0,-50,0'")));
dictionary = new ResourceDictionary();
dictionary.Add("DataPointStyle", style);
palette.Add(dictionary);
barChart.Palette = palette;
barChart.Series.Add(obj_columsrs);
((CustomColumnSeries)barChart.Series[3]).ItemsSource = cust3;
stkpanel.Children.Add(barChart);
if (i == taskdata.Count - 1)
{
stckprint.Children.Add(stkpanel);
}
else if ((i + 1) % taskcount == 0 && i != 0)
{
stckprint.Children.Add(stkpanel);
}
}
}
catch (Exception)
{
}
}
this is the sample code of print button click
private void Print_Click(object sender, RoutedEventArgs e)
{
try
{
PrintDocument pd = new PrintDocument();
int itemIndex = 0;
pd.PrintPage += (s, pe) =>
{
while (itemIndex < stckprint.Children.Count)
{
try
{
stckprint.Measure(new Size(pe.PrintableArea.Width, double.PositiveInfinity));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
if (stckprint.DesiredSize.Height > pe.PrintableArea.Height && stckprint.Children.Count > 1 && stckprint.Children.Count >1)
{
stckprint.Children.Remove(stckprint);
pe.HasMorePages = true;
break;
}
if (stckprint.DesiredSize.Width > pe.PrintableArea.Width && stckprint.Children.Count > 1)
{
stckprint.Children.Remove(stckprint);
pe.HasMorePages = true;
break;
}
}
pe.PageVisual = stckprint;
itemIndex += 1;
};
Viewbox viewbox = new Viewbox();
ItemsControl itemhost = new ItemsControl();
pd.Print("text");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
plase try to give me complete solution based on this code sample that will work fine
thanks and regard.
Vikash