我想创建一个图表来获取产品的数量和产品的销售额,并在可能的情况下显示图表中的销售百分比。
例如ProductA
数量:100,ProductA
销售额:50。所以50%的销售额。
我现在有这个工作图表,它总结了产品的所有销售额,但没有显示数量。 (数量来自名为Products的不同表格)
如果我想要那些工作,我想要的例子,在图表产品中显示仅超过50%的销售额或产品仅低于50%的销售额。
private void loadchartFastt()
{
chart1.Series[0].Points.Clear();
chart1.ChartAreas["ChartArea1"].AxisX.Interval = 1;
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["cnn"].ConnectionString))
{
if (cnn.State == ConnectionState.Closed)
cnn.Open();
SqlCommand command = new SqlCommand("SELECT TOP 5 ProductName, Sum(QtySold) as QtySold FROM Sales_productholder group by ProductName order by SUM(QtySold) desc", cnn); //top selling with desc
SqlDataReader read = command.ExecuteReader();
while (read.Read())
{
this.chart1.Series["Pieces Sold"].Points.AddXY(read["ProductName"], read["QtySold"]);
}
read.Close();
}
}
答案 0 :(得分:1)
为了达到这个目的,你可以试试这个:
[Total Sold Quantity]/[Quantity] * 100
获取销售百分比。 注意:我们必须将
sp.QtySold
和Quantity
转换为浮动才能获得 得到小数点的结果。然后你可以将表达式括起来 到DECIMAL
只显示两个小数点。
请尝试以下代码:
var sql = @"SELECT TOP 5
sp.ProductName,
SUM(sp.QtySold) AS QtySold,
p.Quantity,
CAST((CAST(SUM(sp.QtySold) AS FLOAT) / CAST(p.Quantity AS FLOAT)) * 100 AS DECIMAL(8,2)) [SalesPercentage]
FROM
Sales_productholder sp
JOIN Products p ON (sp.ProductID = p.ProductID)
GROUP BY
sp.ProductName, p.ProductID, p.Quantity
ORDER BY
SUM(sp.QtySold) DESC";
SqlCommand command = new SqlCommand(sql, cnn); //top selling with desc
SqlDataReader read = command.ExecuteReader();
while (read.Read())
{
this.chart1.Series["Pieces Sold"].Points.AddXY(read["ProductName"], read["QtySold"]);
//add another series for the sold %
this.chart1.Series["Sold Percentage"].Points.AddXY(read["ProductName"], read["SalesPercentage"]);
}
您可以尝试下载此源代码以供参考: https://github.com/makubex88/SampleChartWinform