📄 chart.cs
字号:
/** Copyright (c) 2006, All-In-One Creations, Ltd.* All rights reserved.* * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:* * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.* * Neither the name of All-In-One Creations, Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.* * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AREDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLEFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIALDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ORSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVERCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.**//**
* Project: emergetk: stateful web framework for the masses
* File name: Chart.cs
* Description: databound charting widget, currently only renders to svg. We could build a IGroup interface, but that
* might be to closely bound to the svg paradigm. Definitely want ILine, ICircle, IRect, etc. But the IGroup is really nice,
* to aggregate matrix transforms, so it may be worth building an IGroup interface, even in output mediums that don't support it.
*
* Author: Ben Joldersma
*
* @see The GNU Public License (GPL)
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
using System;
using System.Drawing;
using EmergeTk.Model;
using System.ComponentModel;
using System.Collections.Generic;
using EmergeTk.Widgets.Svg;
namespace EmergeTk.Widgets.Html
{
public enum ChartType
{
Bar,
Line,
Scatter,
Pie
}
/// <summary>
/// Summary description for Chart.
/// </summary>
public class Chart<T> : Host, IDataSourced where T : AbstractRecord
{
public event DelayedMouseOverHandler OnNodeDelayedMouseOverHandler;
public event DelayedMouseOutHandler OnNodeDelayedMouseOutHandler;
public event OnClickHandler OnNodeClickedHandler;
public event ReceiveDropHandler
OnXReceiveDropHandler,
OnYReceiveDropHandler,
OnColorReceiveDropHandler,
OnSizeReceiveDropHandler;
private string xSeries;
private bool xRequiresBinding = false;
/// <summary>
/// Property XSources (string)
/// </summary>
public string XSeries
{
get
{
return this.xSeries;
}
set
{
this.xSeries = value;
xRequiresBinding = true;
}
}
private string ySeries;
private bool yRequiresBinding = false;
/// <summary>
/// Property YSources (string)
/// </summary>
public string YSeries
{
get
{
return this.ySeries;
}
set
{
this.ySeries = value;
yRequiresBinding = true;
}
}
private string sizeSeries;
private bool sizeRequiresBinding = false;
public string SizeSeries
{
get { return sizeSeries; }
set { sizeSeries = value; sizeRequiresBinding = true; }
}
private string colorSeries;
private bool colorRequiresBinding = false;
public string ColorSeries
{
get { return colorSeries; }
set { colorSeries = value; colorRequiresBinding = true; }
}
private string yLabel;
/// <summary>
/// Property YLabel (string)
/// </summary>
public string YLabel
{
get
{
return this.yLabel;
}
set
{
this.yLabel = value;
}
}
private string xLabel;
/// <summary>
/// Property XLabel (string)
/// </summary>
public string XLabel
{
get
{
return this.xLabel;
}
set
{
this.xLabel = value;
}
}
private ChartType type;
/// <summary>
/// Property Type (ChartType)
/// </summary>
public ChartType Type
{
get
{
return this.type;
}
set
{
this.type = value;
}
}
private IRecordList<T> dataSource;
/// <summary>
/// Property DataSource (RecordList)
/// </summary>
public IRecordList<T> DataSource
{
get
{
return this.dataSource;
}
set
{
this.dataSource = value;
}
}
private Dictionary<T,Widget> recordToPoints = new Dictionary<T,Widget>();
public Dictionary<T,Widget> RecordToPoints
{
get { return recordToPoints; }
set { recordToPoints = value; }
}
private Rect background;
public Rect Background { get { return background; } }
public Chart()
{
this.ClientClass = "SvgHost";
}
private Group g;
float
xmin, xmax, xdelta,
ymin, ymax, ydelta,
colormin, colormax, colordelta,
sizemin, sizemax, sizedelta;
Type xType, yType;
public void DataBind()
{
if( dataSource == null || dataSource.Count == 0 )
return;
findMinMax(xSeries, out xmin, out xmax, out xdelta);
findMinMax(ySeries, out ymin, out ymax, out ydelta);
findMinMax(colorSeries, out colormin, out colormax, out colordelta);
findMinMax(sizeSeries, out sizemin, out sizemax, out sizedelta);
if (!rendered)
{
setupGradients();
g = root.CreateWidget<Group>();
g.Scale(1, -1);
g.Translate(0, -900);
background = g.DrawRect(0, 0, 1000, 1000, "url(#backgroundGradient)");
for (int i = 0; i <= 10; i++)
{
Line gridX = new Line("xgrid" + i, i * 100, 0, i * 100, 1000, Color.DarkGray);
Line gridY = new Line("xgrid" + i, 0, i * 100, 1000, i * 100, Color.DarkGray);
g.Add(gridX, gridY);
}
g.DrawLine(0, 0, 0, 1000, Color.Black);
g.DrawLine(0, 0, 1000, 0, Color.Black);
Add(g);
//drag regions
if (OnXReceiveDropHandler != null && OnYReceiveDropHandler != null)
{
Rect yAxisDropTarget = new Rect("yAxisDropTarget", -50, 0, 50, 1000, "blue");
yAxisDropTarget.ClassName = "dragTarget";
yAxisDropTarget.OnReceiveDrop += OnYReceiveDropHandler;
Rect xAxisDropTarget = new Rect("xAxisDropTarget", 0, -50, 1000, 50, "blue");
xAxisDropTarget.ClassName = "dragTarget";
xAxisDropTarget.OnReceiveDrop += OnXReceiveDropHandler;
g.Add(xAxisDropTarget, yAxisDropTarget);
}
}
addLegend();
xType = DataSource[0][xSeries].GetType();
yType = DataSource[0][ySeries].GetType();
ViewBox = new Rectangle( -100, -100, 1200, 1100 );
foreach (T r in dataSource)
DrawPoint(r);
xRequiresBinding = yRequiresBinding = sizeRequiresBinding = colorRequiresBinding = false;
setLabels();
dataBound = true;
}
bool dataBound = false;
public bool IsDataBound { get { return dataBound; } set { dataBound = value; } }
public void DrawPoint(T r)
{
float x = coerceValue(r[xSeries]);
float y = coerceValue(r[ySeries]);
float color = coerceValue(r[colorSeries]);
float size = coerceValue(r[sizeSeries]);
x = prepareValue(xmin, xdelta, x, 1000);
y = prepareValue(ymin, ydelta, y, 1000);
size = prepareValue(sizemin, sizedelta, size, 25) + 5;
string colorString = colorFromRange(colormin, colormax, colordelta, color);
if (!rendered)
{
Circle c = g.DrawCircle(x, y, size, colorString, "black");
if (OnNodeDelayedMouseOverHandler != null)
c.OnDelayedMouseOver += OnNodeDelayedMouseOverHandler;
if (OnNodeDelayedMouseOutHandler != null)
c.OnDelayedMouseOut += OnNodeDelayedMouseOutHandler;
if( OnNodeClickedHandler != null )
c.OnClick += OnNodeClickedHandler;
c.Record = r;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -