Applies to: Nevron Gauge for SharePoint (WSS3.0, SharePoint 2007/2010/2013)
How to set the color of the pointer based on the color of the ranges in the Gauge web part?
In Nevron Gauge for SharePoint you can change the color of a gauge pointer(s) (needle or marker) when the pointer is in specific range. This feature can be used as another way to indicate a specific status based on the values passed to the gauge (similar to the Gauge State Indicator).
Let’s say that we have added 3 ranges with different fill styles (Green, Yellow and Red) to a radial gauge:
The following example code will check for the Indicators (Value Indicators and Range Indicators) and will color the pointer (gauge needle) when it is in specific gauge range, using the color of this range. The code will do the same with the gauge Cap.
[C#]
using
System;
using
System.Drawing;
using
Nevron.GraphicsCore;
using
Nevron.Chart;
using
Nevron.ReportingServices;
namespace
MyNamespace
{
/// <summary>
/// Sample class
/// </summary>
public
class
MyClass
{
/// <summary>
/// Main entry point
/// </summary>
/// <param name="context"></param>
public
static
void
RSMain(NRSGaugeCodeContext context)
{
// check if gauge document contains gauges
if
(context.Document.Gauges.Count == 0)
return
;
// the needle indicator is after the range
NGaugePanel gauge = context.Document.Gauges[0]
as
NGaugePanel;
if
(gauge ==
null
|| gauge.Indicators.Count == 0)
return
;
NRadialGaugePanel radialGauge = gauge
as
NRadialGaugePanel;
int
indicatorCount = gauge.Indicators.Count;
NValueIndicator needle =
null
;
for
(
int
i = 0; i < indicatorCount; i++)
{
needle = gauge.Indicators[i]
as
NValueIndicator;
if
(needle !=
null
)
break
;
}
if
(needle ==
null
)
return
;
int
rangIndicatorIndex = 0;
for
(
int
i = 0; i < indicatorCount; i++)
{
NRangeIndicator rangeIndicator = gauge.Indicators[i]
as
NRangeIndicator;
if
(rangeIndicator !=
null
)
{
NRange1DD range =
new
NRange1DD(rangeIndicator.Origin, rangeIndicator.Value);
range.Normalize();
// if indicator value is contained by the range change its color to red
if
(range.Contains(needle.Value))
{
//needle.Shape.FillStyle = GetFillStyleForRange(rangIndicatorIndex);
needle.Shape.FillStyle = rangeIndicator.FillStyle;
if
(radialGauge !=
null
)
{
//radialGauge.CapStyle.Shape.FillStyle = GetFillStyleForRange(rangIndicatorIndex);
radialGauge.CapStyle.Shape.FillStyle = rangeIndicator.FillStyle;
}
break
;
}
rangIndicatorIndex++;
}
}
}
//static NFillStyle GetFillStyleForRange(int rangeIndex)
//{
// switch (rangeIndex)
// {
// case 0:
// return new NColorFillStyle(Color.Green);
// case 1:
// return new NColorFillStyle(Color.Yellow);
// case 2:
// return new NColorFillStyle(Color.Red);
// default:
// return new NColorFillStyle(Color.Black);
// }
//}
}
}
[VB.NET]
Imports
System
Imports
System.Drawing
Imports
Nevron.GraphicsCore
Imports
Nevron.Chart
Imports
Nevron.ReportingServices
Namespace
MyNamespace
''' <summary>
''' Sample class
''' </summary>
Public
Class
[
MyClass
]
''' <summary>
''' Main entry point
''' </summary>
''' <param name="context"></param>
Public
Shared
Sub
RSMain(context
As
NRSGaugeCodeContext)
' check if gauge document contains gauges
If
context.Document.Gauges.Count = 0
Then
Return
End
If
' the needle indicator is after the range
Dim
gauge
As
NGaugePanel = TryCast(context.Document.Gauges(0), NGaugePanel)
If
gauge
Is
Nothing
OrElse
gauge.Indicators.Count = 0
Then
Return
End
If
Dim
radialGauge
As
NRadialGaugePanel = TryCast(gauge, NRadialGaugePanel)
Dim
indicatorCount
As
Integer
= gauge.Indicators.Count
Dim
needle
As
NValueIndicator =
Nothing
For
i
As
Integer
= 0
To
indicatorCount - 1
needle = TryCast(gauge.Indicators(i), NValueIndicator)
If
needle IsNot
Nothing
Then
Exit
For
End
If
Next
If
needle
Is
Nothing
Then
Return
End
If
Dim
rangIndicatorIndex
As
Integer
= 0
For
i
As
Integer
= 0
To
indicatorCount - 1
Dim
rangeIndicator
As
NRangeIndicator = TryCast(gauge.Indicators(i), NRangeIndicator)
If
rangeIndicator IsNot
Nothing
Then
Dim
range
As
New
NRange1DD(rangeIndicator.Origin, rangeIndicator.Value)
range.Normalize()
' if indicator value is contained by the range change its color to red
If
range.Contains(needle.Value)
Then
'needle.Shape.FillStyle = GetFillStyleForRange(rangIndicatorIndex);
needle.Shape.FillStyle = rangeIndicator.FillStyle
If
radialGauge IsNot
Nothing
Then
'radialGauge.CapStyle.Shape.FillStyle = GetFillStyleForRange(rangIndicatorIndex);
radialGauge.CapStyle.Shape.FillStyle = rangeIndicator.FillStyle
End
If
Exit
For
End
If
rangIndicatorIndex += 1
End
If
Next
End
Sub
'static NFillStyle GetFillStyleForRange(int rangeIndex)
'{
' switch (rangeIndex)
' {
' case 0:
' return new NColorFillStyle(Color.Green);
' case 1:
' return new NColorFillStyle(Color.Yellow);
' case 2:
' return new NColorFillStyle(Color.Red);
' default:
' return new NColorFillStyle(Color.Black);
' }
'}
End
Class
End
Namespace
If you use the commented code in the bottom instead, you could specify the color of the pointer and the cap separately. Note that in this case, if you have more Ranges, you will have to specify additional cases in the code, otherwise the needle will take the default color.
Article ID: 213, Created On: 3/19/2012, Modified: 1/29/2013