简体   繁体   中英

How to create a chart in ASP.NET using SQL Server

I want to create a bar chart, that displays which columns has the most values.

This is my query

SELECT 
    SUM(CASE WHEN radioButtonLog = 1 THEN 1 END) AS 'Super Happy', 
    SUM(CASE WHEN radioButtonLog = 2 THEN 1 END) AS 'Happy', 
    SUM(CASE WHEN radioButtonLog = 3 THEN 1 END) AS 'Normal', 
    SUM(CASE WHEN radioButtonLog = 4 THEN 1 END) AS 'Sad',
    SUM(CASE WHEN radioButtonLog = 5 THEN 1 END) AS 'Super Sad'
FROM 
    tblMessage

在此处输入图像描述

This is the output - now I want to display these values in a chart.

<asp:Chart ID="Chart1" runat="server" DataSourceID="SqlDataSource1">
    <Series>
        <asp:Series Name="Series1" XValueMember="Super Happy" YValueMembers="Super Happy"></asp:Series>
        <asp:Series Name="Series2" XValueMember="Happy" YValueMembers="Happy"></asp:Series>
        <asp:Series Name="Series3" XValueMember="Normal" YValueMembers="Normal"></asp:Series>
        <asp:Series Name="Series4" XValueMember="Sad" YValueMembers="Sad"></asp:Series>
        <asp:Series Name="Series5" XValueMember="Super Sad" YValueMembers="Super Sad"></asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
    </ChartAreas>
</asp:Chart>

在此处输入图像描述

This is the output. I understand, that the XValueMember is wrong, because I put the same values as Y axis, but instead of displaying numbers in X axis, how can I display "Super Happy", "Happy", "Normal" etc. ?

You're adding multiple Series to the chart, which is incorrect. You need to add a "Points" collection to the series, then a DataPoint for each result.

https://www.4guysfromrolla.com/articles/072209-1.aspx <-- this is a good guide to using the chart component.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM