public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static List<SmartPhoneVendor> vendors = new List<SmartPhoneVendor>();
static Image LegendIconImage = Properties.Resources.usa;
Series customSeries;
private void Form1_Load(object sender, EventArgs e)
{
vendors = SmartPhoneVendors();
// データソースを設定します
flexChart1.DataSource = vendors;
flexChart1.BindingX = "Name";
flexChart1.Binding = "Sales";
flexChart1.Header.Style.Font = new Font(FontFamily.GenericSansSerif, 15);
flexChart1.Header.Content = "Top Smartphone Vendors";
flexChart1.Series.Clear();
// カスタム系列を追加します
customSeries = new SeriesWithPointLegendItems();
customSeries.Name = "Sales in USA";
flexChart1.Series.Add(customSeries);
flexChart1.Legend.Position = Position.Left;
flexChart1.ToolTip.Content = "{seriesName}\r\n{value}";
}
public class SeriesWithPointLegendItems : Series, ISeries
{
object ISeries.GetLegendItemImageSource(int index, ref C1.Chart._Size imageSize)
{
{
// 元の画像/ロゴはすべて50x50ピクセルです
// ここでは、ロゴの周りに5ピクセルのボーダーが追加された新しいイメージに置き換えられます
imageSize.Height = 60;
imageSize.Width = 60;
SmartPhoneVendor vendor = vendors.ElementAt(index);
if (LegendIconImage != null && LegendIconImage.Width != 60)
{
Bitmap bmp = new Bitmap(60, 60);
using (SolidBrush sb = new SolidBrush(vendor.Color))
{
using (Graphics g = Graphics.FromImage(bmp))
{
Rectangle r = new Rectangle(0, 0, (int)imageSize.Width, (int)imageSize.Height);
using (Pen p = new Pen(sb))
{
g.DrawRectangle(p, r);
}
g.FillRectangle(sb, r);
Point ci = new Point((int)(0.5 * (imageSize.Width - LegendIconImage.Width)),
(int)(0.5 * (imageSize.Height - LegendIconImage.Height)));
g.DrawImage(LegendIconImage, ci);
}
}
LegendIconImage = bmp;
}
// ロゴビットマップの元のサイズを維持しますが、グラフウィンドウがビットマップを適切に表示するには
// 小さすぎる場合は、サイズを縮小します
Size bounds = this.Chart.ClientSize;
double divadj = (bounds.Height > 800) ? 12 : 25;
double fracHeight = bounds.Height / divadj;
if (fracHeight < imageSize.Height)
imageSize.Width = imageSize.Height = fracHeight;
return LegendIconImage;
}
}
}
private static List<SmartPhoneVendor> SmartPhoneVendors()
{
vendors.Add(new SmartPhoneVendor()
{
Name = "Apple",
Color = Color.FromArgb(136, 189, 230),
Sales = 350,
});
vendors.Add(new SmartPhoneVendor()
{
Name = "LG",
Color = Color.FromArgb(251, 178, 88),
Sales = 120,
});
vendors.Add(new SmartPhoneVendor()
{
Name = "Samsung",
Color = Color.FromArgb(188, 153, 199),
Sales = 280,
});
vendors.Add(new SmartPhoneVendor()
{
Name = "Xiaomi",
Color = Color.FromArgb(240, 126, 110),
Sales = 68,
});
return vendors;
}
public class SmartPhoneVendor
{
public string Name { get; set; }
public double Sales { get; set; }
public Color Color { get; set; }
}
}