Common Custom renderers
Common_CustomRenderers_VBNet\App_Code\GraduatedColorRenderer.vb
' Copyright 2010 ESRI
' 
' All rights reserved under the copyright laws of the United States
' and applicable international laws, treaties, and conventions.
' 
' You may freely redistribute and use this sample code, with or
' without modification, provided you include the original copyright
' notice and use restrictions.
' 
' See the use restrictions.
' 

Imports Microsoft.VisualBasic
Imports System
Namespace ESRI.ADF.Samples.Renderers
  ''' <summary>
  ''' Renderer that colors a layer based on an attribute
  ''' </summary>
  <System.Serializable> _
  Public Class GraduatedColorRenderer
    Inherits ESRI.ADF.Samples.Renderers.RendererBase
    #Region "Public Properties"

    Private startColor_Renamed As System.Drawing.Color = System.Drawing.Color.Red

    ''' <summary>
    ''' Beginning color of the renderer's gradient
    ''' </summary>
    Public Property StartColor() As System.Drawing.Color
      Get
        Return startColor_Renamed
      End Get
      Set
        startColor_Renamed = Value
      End Set
    End Property

    Private endColor_Renamed As System.Drawing.Color = System.Drawing.Color.Yellow

    ''' <summary>
    ''' Ending color of the renderer's gradient
    ''' </summary>
    Public Property EndColor() As System.Drawing.Color
      Get
        Return endColor_Renamed
      End Get
      Set
        endColor_Renamed = Value
      End Set
    End Property

    Private minValue_Renamed As Double = 0

    ''' <summary>
    ''' The value to apply StartColor to.  Values less than MinValue will also
    ''' be rendered using StartColor.
    ''' </summary>
    Public Property MinValue() As Double
      Get
        Return minValue_Renamed
      End Get
      Set
        minValue_Renamed = Value
      End Set
    End Property

    Private maxValue_Renamed As Double = 100

    ''' <summary>
    ''' The value to apply EndColor to.  Values greater than MaxValue will also
    ''' be rendered using EndColor.
    ''' </summary>
    Public Property MaxValue() As Double
      Get
        Return maxValue_Renamed
      End Get
      Set
        maxValue_Renamed = Value
      End Set
    End Property

    Private colorColumnName_Renamed As String

    ''' <summary>
    ''' The name of the column to use in determining a feature's color
    ''' </summary>
    Public Property ColorColumnName() As String
      Get
        Return colorColumnName_Renamed
      End Get
      Set
        colorColumnName_Renamed = Value
      End Set
    End Property

    #End Region

    #Region "IRenderer Members"

    ''' <summary>
    ''' Main part of the IRenderer interface, within which a feature encapsulating the specified DataRow is to be 
    ''' rendered on the specified graphics surface. The geometry instance has already been transformed to screen 
    ''' coordinate, so we don't have to worry about that here.
    ''' </summary>
    ''' <param name="row">row containing the feature's data</param>
    ''' <param name="graphics">GDI+ surface on which to render the feature</param>
    ''' <param name="geometryColumn">column containing the feature's geometry</param>
    Public Overrides Sub Render(ByVal row As System.Data.DataRow, ByVal graphics As System.Drawing.Graphics, ByVal geometryColumn As System.Data.DataColumn)
      ' Validate input
      If row Is Nothing OrElse graphics Is Nothing OrElse geometryColumn Is Nothing Then
        Return
      End If

      ' Get the feature geometry
      Dim adfGeometry As ESRI.ArcGIS.ADF.Web.Geometry.Geometry = TryCast(row(geometryColumn), ESRI.ArcGIS.ADF.Web.Geometry.Geometry)

      ' Validate the geometry.  Points are not handled by this renderer.
      If adfGeometry Is Nothing OrElse TypeOf adfGeometry Is ESRI.ArcGIS.ADF.Web.Geometry.Point Then
        Return
      End If

      ' Make sure the feature has the column specified for use in determining the feature's color
      If (Not String.IsNullOrEmpty(Me.ColorColumnName)) AndAlso row.Table.Columns.Contains(Me.ColorColumnName) Then
        ' Make sure the color value can be cast to a double
        Dim value As Double = 0
        If Double.TryParse(row(ColorColumnName).ToString(), value) Then
          ' Calculate what color the value corresponds to
          Dim color As System.Drawing.Color = interpolateColor(value)

          ' Render the geometry with the renderers utility
          If TypeOf adfGeometry Is ESRI.ArcGIS.ADF.Web.Geometry.Polygon Then
            ESRI.ADF.Samples.Renderers.Utility.FillPolygon(graphics, TryCast(adfGeometry, ESRI.ArcGIS.ADF.Web.Geometry.Polygon), color, 0, 0, 0)
          ElseIf TypeOf adfGeometry Is ESRI.ArcGIS.ADF.Web.Geometry.Polyline Then
            ESRI.ADF.Samples.Renderers.Utility.DrawPolyline(graphics, TryCast(adfGeometry, ESRI.ArcGIS.ADF.Web.Geometry.Polyline), color, 2)
          End If
        End If
      End If
    End Sub

    ''' <summary>
    ''' Creates swatches used for the Table of Contents / Legend.
    ''' This is automatically called by IMapTocFunctionality when generating the TOC.
    ''' </summary>
    ''' <param name="swatchInfo"></param>
    ''' <param name="fileName"></param>
    ''' <param name="minScale"></param>
    ''' <param name="maxScale"></param>
    ''' <returns></returns>
    Public Overrides Function GenerateSwatches(ByVal swatchInfo As ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchInfo, ByVal fileName As String, ByVal minScale As String, ByVal maxScale As String) As ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchCollection
      Dim swatches As ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchCollection = MyBase.GenerateSwatches(swatchInfo, fileName, minScale, maxScale)
      Dim swatchUtility As ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchUtility = New ESRI.ArcGIS.ADF.Web.Display.Swatch.SwatchUtility(swatchInfo)

      Dim swatchCount As Integer = 5 ' The number of swatches created is this number plus one

      ' Create the swatches
      Dim i As Integer = 0
      Do While i <= swatchCount
        ' Use the renderer's man and max values to interpolate the value at step 'i'
        Dim value As Double = minValue_Renamed + (maxValue_Renamed - minValue_Renamed) / swatchCount * i

        ' Get the color at this value
        Dim color As System.Drawing.Color = interpolateColor(value)

        ' Create the swatch image
        Dim swatchImage As ESRI.ArcGIS.ADF.Web.CartoImage = swatchUtility.DrawNewSwatch(New ESRI.ArcGIS.ADF.Web.Display.Symbol.SimpleFillSymbol(color, System.Drawing.Color.Empty, ESRI.ArcGIS.ADF.Web.Display.Symbol.PolygonFillType.Solid), fileName)

        ' Create the swatch and add it to the collection
        swatches.Add(New ESRI.ArcGIS.ADF.Web.Display.Swatch.Swatch(swatchImage, value.ToString(), Nothing, Nothing))
        i += 1
      Loop
      Return swatches
    End Function

    #End Region

    ''' <summary>
    ''' Returns the color for a given value based on the renderer's min and max values and start and end colors.
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    Private Function interpolateColor(ByVal value As Double) As System.Drawing.Color
      ' Return the start or end color if the value is at or outside the renderer's value bounds
      If value <= minValue_Renamed Then
      Return startColor_Renamed
      End If
      If value >= maxValue_Renamed Then
      Return endColor_Renamed
      End If

      ' Calculate how far along the gradient the passed-in value is
      Dim distanceAlongGradient As Double = (value - minValue_Renamed) / (maxValue_Renamed - minValue_Renamed)

      ' Interpolate the red, green, blue, and alpha values based on the distance along the gradient,
      ' the renderer's start color, and the renderer's end color
      Dim red As Integer = CInt(Fix(System.Math.Round(startColor_Renamed.R * (1 - distanceAlongGradient) + endColor_Renamed.R * (distanceAlongGradient))))
      Dim green As Integer = CInt(Fix(System.Math.Round(startColor_Renamed.G * (1 - distanceAlongGradient) + endColor_Renamed.G * (distanceAlongGradient))))
      Dim blue As Integer = CInt(Fix(System.Math.Round(startColor_Renamed.B * (1 - distanceAlongGradient) + endColor_Renamed.B * (distanceAlongGradient))))
      Dim alpha As Integer = CInt(Fix(System.Math.Round(startColor_Renamed.A * (1 - distanceAlongGradient) + endColor_Renamed.A * (distanceAlongGradient))))

      Return System.Drawing.Color.FromArgb(alpha, red, green, blue)
    End Function
  End Class
End Namespace