Hi nsds,
You need to Loop through each series data points and set IsValueShownAsLabel as false if the value is zero.
Check below example.
HTML
<asp:Chart ID="Chart1" runat="server" Height="300px" Width="400px">
<Titles>
<asp:Title ShadowOffset="0" Name="Items" Text="TEST STATUS" />
</Titles>
<Series>
<asp:Series Name="In Progress" IsValueShownAsLabel="true" ChartType="StackedBar"></asp:Series>
<asp:Series Name="Assigned" IsValueShownAsLabel="true" ChartType="StackedBar"></asp:Series>
<asp:Series Name="Prepare" IsValueShownAsLabel="true" ChartType="StackedBar"></asp:Series>
<asp:Series Name="Completed" IsValueShownAsLabel="true" ChartType="StackedBar"></asp:Series>
</Series>
<Legends>
<asp:Legend Alignment="Center" Docking="Bottom" IsTextAutoFit="False" Name="Default"
LegendStyle="Row" />
</Legends>
<ChartAreas>
<asp:ChartArea Name="ChartArea1" BorderWidth="0">
<AxisY>
<MajorGrid Enabled="False" />
</AxisY>
<AxisX>
<MajorGrid Enabled="false" />
</AxisX>
</asp:ChartArea>
</ChartAreas>
</asp:Chart>
Namespaces
C#
using System.Data;
using System.Web.UI.DataVisualization.Charting;
VB.Net
Imports System.Data
Imports System.Web.UI.DataVisualization.Charting
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable data_tasks = new DataTable();
data_tasks.Columns.AddRange(new DataColumn[3]
{
new DataColumn("total", typeof(int)),
new DataColumn("task_status_name", typeof(string)),
new DataColumn("type_of_task",typeof(string))
});
data_tasks.Rows.Add(1, "In Progress", "Test1");
data_tasks.Rows.Add(2, "Assigned", "Test2");
data_tasks.Rows.Add(3, "Prepare", "Test3");
data_tasks.Rows.Add(4, "Assigned", "Test2");
data_tasks.Rows.Add(5, "In Progress", "Test4");
data_tasks.Rows.Add(6, "In Progress", "Test2");
data_tasks.Rows.Add(8, "In Progress", "Test5");
//Get the DISTINCT task_statuses.
List<string> task_status_list = (from p in data_tasks.AsEnumerable()
select p.Field<string>("task_status_name")).Distinct().ToList();
//Loop through the task_statuses.
foreach (string task_Status in task_status_list)
{
string[] x = (from p in data_tasks.AsEnumerable()
select p.Field<string>("type_of_task")).Distinct().ToArray();
List<int> y = new List<int>();
foreach (string dr in x)
{
DataRow datarow = (from p in data_tasks.AsEnumerable()
where p.Field<string>("task_status_name") == task_Status
&& p.Field<string>("type_of_task") == dr
select p).FirstOrDefault();
y.Add(datarow != null ? Convert.ToInt32(datarow["total"]) : 0);
}
Chart1.Series[task_Status].Points.DataBindXY(x, y.ToArray());
}
foreach (Series series in Chart1.Series)
{
foreach (DataPoint point in series.Points)
{
if (point.YValues[0] == 0)
{
point.IsValueShownAsLabel = false;
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim data_tasks As DataTable = New DataTable()
data_tasks.Columns.AddRange(New DataColumn(2) {
New DataColumn("total", GetType(Integer)),
New DataColumn("task_status_name", GetType(String)),
New DataColumn("type_of_task", GetType(String))})
data_tasks.Rows.Add(1, "In Progress", "Test1")
data_tasks.Rows.Add(2, "Assigned", "Test2")
data_tasks.Rows.Add(3, "Prepare", "Test3")
data_tasks.Rows.Add(4, "Assigned", "Test2")
data_tasks.Rows.Add(5, "In Progress", "Test4")
data_tasks.Rows.Add(6, "In Progress", "Test2")
data_tasks.Rows.Add(8, "In Progress", "Test5")
Dim task_status_list As List(Of String) = (From p In data_tasks.AsEnumerable() Select p.Field(Of String)("task_status_name")).Distinct().ToList()
For Each task_Status As String In task_status_list
Dim x As String() = (From p In data_tasks.AsEnumerable() Select p.Field(Of String)("type_of_task")).Distinct().ToArray()
Dim y As List(Of Integer) = New List(Of Integer)()
For Each dr As String In x
Dim datarow As DataRow = (From p In data_tasks.AsEnumerable()
Where p.Field(Of String)("task_status_name") = task_Status _
AndAlso p.Field(Of String)("type_of_task") = dr
Select p).FirstOrDefault()
y.Add(If(datarow IsNot Nothing, Convert.ToInt32(datarow("total")), 0))
Next
Chart1.Series(task_Status).Points.DataBindXY(x, y.ToArray())
Next
For Each series As Series In Chart1.Series
For Each point As DataPoint In series.Points
If point.YValues(0) = 0 Then
point.IsValueShownAsLabel = False
End If
Next
Next
End If
End Sub
Screenshot