当前位置:   article > 正文

amchart图表控件使用_amchart amlegend在左上角显示

amchart amlegend在左上角显示

项目中使用到的amchart对应的js文件及datepicker97控件到以下地址下载:

http://download.csdn.net/detail/taomanman/6889253

效果图如下:


  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="wsdQuery.aspx.cs" Inherits="WSD_wsdQuery" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title>温湿度历史曲线图</title>
  6. <link href="../css/StyleSheet.css" rel="stylesheet" type="text/css" />
  7. <script src="../js/amcharts/amcharts.js" type="text/javascript"></script>
  8. <script src="../js/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
  9. <script src="../js/jquery-1.4.2.min.js" type="text/javascript"></script>
  10. </head>
  11. <body>
  12. <form id="form1" runat="server">
  13. <div class="divtitle">
  14. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  15. <tr>
  16. <td style="text-align: center; letter-spacing: 10px;">
  17. <asp:Label ID="lblTitle" runat="server" Text="1#温度曲线"></asp:Label>
  18. </td>
  19. </tr>
  20. </table>
  21. </div>
  22. <table border="0" cellpadding="0" cellspacing="0" style="margin-left: 0px; vertical-align: middle;"
  23. class="tabSearch">
  24. <tr>
  25. <td>
  26. 监控点名称
  27. </td>
  28. <td>
  29. <asp:DropDownList ID="ddlSiteNM" runat="server" Style="width: 110px">
  30. </asp:DropDownList>
  31. </td>
  32. <td>
  33. 开始时间
  34. </td>
  35. <td>
  36. <input type="text" id="bTM" runat="server" style="width: 100px" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'})"
  37. class="Wdate" />
  38. </td>
  39. <td>
  40. 结束时间
  41. </td>
  42. <td>
  43. <input type="text" id="eTM" runat="server" style="width: 100px" οnfοcus="WdatePicker({dateFmt:'yyyy-MM-dd',maxDate:'%y-%M-%d'})"
  44. class="Wdate" />
  45. </td>
  46. <td>
  47. <asp:Button ID="btnQuery" runat="server" CssClass="btnStyle" Text="查询" OnClick="btnQuery_Click" />
  48. </td>
  49. </tr>
  50. </table>
  51. <div style="margin: 0px 2px; border: 1px solid #0066FF">
  52. <div style="width: 99%; text-align: center; padding: 5px; color: Black; font-size: 17px;
  53. font-weight: bold;">
  54. <asp:Label ID="lbl_data" runat="server"></asp:Label>
  55. </div>
  56. <div style="width: 99%; text-align: center; padding: 5px; color: Black; font-size: 14px;">
  57. <table width="100%" border="0" cellpadding="0" cellspacing="0">
  58. <tr>
  59. <td style="width: 33%">
  60. </td>
  61. <td style="width: 34%; text-align: center;">
  62. <asp:Label ID="lbl_subtitle" runat="server"></asp:Label>
  63. </td>
  64. <td style="width: 33%; text-align: right; padding-right: 5px;">
  65. <%-- <asp:Label ID="lbl_bz" runat="server" Text="单位(m)"></asp:Label>--%>
  66. </td>
  67. </tr>
  68. </table>
  69. </div>
  70. <div id="container" runat="server" style="height: 500px;">
  71. </div>
  72. </div>
  73. <div id="progressBar" class="progressBar" runat="server">
  74. 数据加载中,请稍等...</div>
  75. <!--隐藏域赋值开始-->
  76. <input type="hidden" id="ss" runat="server" value="[]" />
  77. <input type="hidden" id="ss_max" runat="server" value="[]" />
  78. <input type="hidden" id="ss_min" runat="server" value="[]" />
  79. <!--温度最大值-->
  80. <input type="hidden" id="wd_max" runat="server" value="0" />
  81. <!--温度最小值-->
  82. <input type="hidden" id="wd_min" runat="server" value="0" />
  83. <!--湿度最大值-->
  84. <input type="hidden" id="sd_max" runat="server" value="0" />
  85. <!--湿度最小值-->
  86. <input type="hidden" id="sd_min" runat="server" value="0" />
  87. <!--Y轴title-->
  88. <input type="hidden" id="valueAxis_title" runat="server" value="0" />
  89. <input type="hidden" id="valueAxis_title1" runat="server" value="0" />
  90. <input type="hidden" id="valueAxis_title2" runat="server" value="0" />
  91. <input type="hidden" id="color" runat="server" value="0" />
  92. <input type="hidden" id="color_max" runat="server" value="0" />
  93. <input type="hidden" id="color_min" runat="server" value="0" />
  94. <!--显示单位-->
  95. <input type="hidden" id="DW" runat="server" value="0" />
  96. <!--隐藏域赋值结束-->
  97. </form>
  98. </body>
  99. </html>
  100. <script type="text/javascript">
  101. $(function () {
  102. //从后台获取拼接的json串,进行eval解析
  103. var ss = eval("(" + $("#ss").val() + ")");
  104. AmCharts.ready(function () {
  105. /*************************************温湿度***********************************/
  106. chart = new AmCharts.AmSerialChart();
  107. //chart图上的图片路径
  108. chart.pathToImages = "../js/amcharts/images/";
  109. chart.zoomOutButton = {
  110. backgroundColor: '#000000',
  111. backgroundAlpha: 0.15
  112. };
  113. chart.dataProvider = ss;
  114. chart.categoryField = "date";
  115. chart.balloon.bulletSize = 5;
  116. chart.height = "100%";
  117. chart.borderAlpha = 0;
  118. chart.borderColor = "#0066FF";
  119. chart.addListener("dataUpdated", zoomChart);
  120. // AXES
  121. // category
  122. var categoryAxis = chart.categoryAxis;
  123. categoryAxis.dashLength = 1;
  124. categoryAxis.gridAlpha = 0.5; //X轴参考线的透明度
  125. categoryAxis.position = "bottom"; //X轴标签显示的位置
  126. categoryAxis.axisColor = "#DADADA";
  127. categoryAxis.tickLength = 5;
  128. categoryAxis.axisAlpha = 0.9; //X轴刻度线的透明度
  129. categoryAxis.axisColor = "#000000";
  130. categoryAxis.showFirstLabel = false;
  131. categoryAxis.gridPosition = "start"; //控制参考线显示的位置,start在点或者柱状图前显示,middle在点或者柱状图中间显示
  132. categoryAxis.labelRotation = 35;
  133. // value
  134. var valueAxis = new AmCharts.ValueAxis();
  135. valueAxis.title = $("#valueAxis_title").val() + "(" + $("#DW").val() + ")";
  136. valueAxis.axisAlpha = 1;
  137. valueAxis.dashLength = 0;
  138. valueAxis.labelFunction = labelFunction; //控制Y轴显示数据格式
  139. // valueAxis.maximum = $("#wd_max").val();
  140. // valueAxis.minimum = $("#wd_min").val();
  141. chart.addValueAxis(valueAxis);
  142. // GRAPH
  143. var graph = new AmCharts.AmGraph();
  144. graph.title = $("#valueAxis_title").val(); //用于在图例上显示的名称
  145. graph.type = "line";
  146. graph.lineColor = $("#color").val();
  147. graph.valueField = "W"; //图表关联数据源中对应的字段值
  148. graph.balloonText = $("#valueAxis_title").val() + ":[[W]] " + $("#DW").val(); //用于控制图表弹出窗口的信息。
  149. graph.valueAxis = valueAxis; //图表对应的Y轴
  150. graph.bullet = "round"; //过程线上点的形状: "none", "round", "square", "triangleUp", "triangleDown", "bubble", "custom"
  151. graph.bulletAlpha = 1; //过程线上点显示的透明度。
  152. graph.bulletBorderThickness = 1; //过程线上点边线的宽度。
  153. graph.bulletOffset = 0.5; //过程线上点显示的透明度。
  154. graph.bulletSize = 5; //过程线上点大小
  155. graph.lineThickness = 2;
  156. $("#ss_min").val() == "" ? graph.negativeBase = $("#ss_max").val() : graph.negativeBase = $("#ss_min").val(); //用户控制参考线的位置
  157. graph.negativeLineColor = "#ff0000"; //低于参考线的颜色
  158. graph.connect = false; //当数据为空时不显示
  159. graph.hideBulletsCount = 500; // this makes the chart to hide bullets when there are more than 50 series in selection
  160. chart.addGraph(graph);
  161. if ($("#ss_max").val() != "") {
  162. var graph1 = new AmCharts.AmGraph();
  163. graph1.title = $("#valueAxis_title1").val(); //用于在图例上显示的名称
  164. graph1.type = "line";
  165. graph1.lineColor = $("#color_max").val();
  166. graph1.valueField = "max"; //图表关联数据源中对应的字段值
  167. graph1.balloonText = $("#valueAxis_title1").val() + ":[[max]] " + $("#DW").val(); //用于控制图表弹出窗口的信息。
  168. graph1.valueAxis = valueAxis; //图表对应的Y轴
  169. graph1.bullet = "round"; //过程线上点的形状: "none", "round", "square", "triangleUp", "triangleDown", "bubble", "custom"
  170. graph1.bulletAlpha = 1; //过程线上点显示的透明度。
  171. graph1.bulletBorderThickness = 1; //过程线上点边线的宽度。
  172. graph1.bulletOffset = 0.5; //过程线上点显示的透明度。
  173. graph1.bulletSize = 1; //过程线上点大小
  174. graph1.lineThickness = 2;
  175. graph1.hidden = false; //控制过程性显示隐藏
  176. graph1.connect = false; //当数据为空时不显示
  177. graph1.hideBulletsCount = 500; // this makes the chart to hide bullets when there are more than 50 series in selection
  178. chart.addGraph(graph1);
  179. }
  180. if ($("#ss_min").val() != "") {
  181. var graph2 = new AmCharts.AmGraph();
  182. graph2.title = $("#valueAxis_title2").val(); //用于在图例上显示的名称
  183. graph2.type = "line";
  184. graph2.lineColor = $("#color_min").val();
  185. graph2.valueField = "min"; //图表关联数据源中对应的字段值
  186. graph2.balloonText = $("#valueAxis_title2").val() + ":[[min]] " + $("#DW").val(); //用于控制图表弹出窗口的信息。
  187. graph2.valueAxis = valueAxis; //图表对应的Y轴
  188. graph2.bullet = "round"; //过程线上点的形状: "none", "round", "square", "triangleUp", "triangleDown", "bubble", "custom"
  189. graph2.bulletAlpha = 1; //过程线上点显示的透明度。
  190. graph2.bulletBorderThickness = 1; //过程线上点边线的宽度。
  191. graph2.bulletOffset = 0.5; //过程线上点显示的透明度。
  192. graph2.bulletSize = 1; //过程线上点大小
  193. graph2.lineThickness = 2;
  194. graph2.hidden = false;
  195. graph2.connect = false; //当数据为空时不显示
  196. graph2.hideBulletsCount = 500; // this makes the chart to hide bullets when there are more than 50 series in selection
  197. chart.addGraph(graph2);
  198. }
  199. // CURSOR
  200. chartCursor = new AmCharts.ChartCursor();
  201. chartCursor.zoomable = true; //是否可以框选放大
  202. chartCursor.categoryBalloonAlpha = 1; //用于控制鼠标移动时X轴显示文本的透明度
  203. chartCursor.cursorAlpha = 1; //0不显示参考线,1显示参考线
  204. chartCursor.valueBalloonsEnabled = true; //控制鼠标移动到图上是否显示弹出框
  205. chart.addChartCursor(chartCursor);
  206. // LEGEND
  207. var legend = new AmCharts.AmLegend();
  208. legend.valueWidth = 0; //图例上显示数值的宽度。
  209. legend.position = "bottom";
  210. legend.valueText = ""; //图例上显示数据的文本,此处为空为了覆盖图上产生的数值。
  211. chart.addLegend(legend);
  212. //滚动条设置
  213. var chartScrollbar = new AmCharts.ChartScrollbar();
  214. chart.addChartScrollbar(chartScrollbar);
  215. // 输出显示Chart图
  216. chart.write("container");
  217. $("#progressBar").css("display", "none");
  218. $("#ss").val("[]")
  219. });
  220. //当chart图表第一次加载时,监听到dataUpdated事件后被调用
  221. function zoomChart() {
  222. //zoom可以使用的函数有:zoomToIndexes, zoomToDates, zoomToCategoryValues
  223. chart.zoomToIndexes(0, ss.length - 1);
  224. }
  225. function labelFunction(a, b, c) {
  226. var ss = parseFloat(b.replace(',', '')).toFixed(2);
  227. return ss;
  228. }
  229. });
  230. </script>
  231. using System;
  232. using System.Collections.Generic;
  233. using System.Linq;
  234. using System.Web;
  235. using System.Web.UI;
  236. using System.Web.UI.WebControls;
  237. using System.Data;
  238. using System.Text;
  239. using USTC;
  240. public partial class WSD_wsdQuery : System.Web.UI.Page
  241. {
  242. OracleDM odm = new OracleDM();
  243. protected void Page_Load(object sender, EventArgs e)
  244. {
  245. if (!IsPostBack)
  246. {
  247. //设置过程线的颜色
  248. this.color.Value = Global.chart_color;
  249. this.color_max.Value = Global.chart_color_max;
  250. this.color_min.Value = Global.chart_color_min;
  251. //初始为2个时间选择框赋值
  252. this.bTM.Value = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
  253. this.eTM.Value = DateTime.Now.ToString("yyyy-MM-dd");
  254. BindDropDownList();
  255. string ColumnName = this.Request.QueryString["ColumnName"] ?? "WENDU";
  256. ViewState["ColumnName"] = ColumnName;
  257. string deviceId = this.ddlSiteNM.SelectedItem.Value.Trim();
  258. string deviceName = this.ddlSiteNM.SelectedItem.Text.Trim();
  259. progressBar.Style["display"] = "block";
  260. SetChartData(deviceId, deviceName, this.bTM.Value, this.eTM.Value, ColumnName);
  261. }
  262. }
  263. /// <summary>
  264. /// 根据条件生成Chart数据
  265. /// </summary>
  266. public void SetChartData(string deviceId, string deviceName, string sDT, string eDT, string ColunmName)
  267. {
  268. switch (ColunmName.ToUpper())
  269. {
  270. case "WENDU":
  271. this.valueAxis_title.Value = "温度";
  272. this.DW.Value = "℃";
  273. break;
  274. case "SHIDU":
  275. this.valueAxis_title.Value = "湿度";
  276. this.DW.Value = "%";
  277. break;
  278. default:
  279. this.valueAxis_title.Value = "温度";
  280. this.DW.Value = "℃";
  281. break;
  282. }
  283. StringBuilder sb = new StringBuilder();
  284. string data = string.Empty;
  285. string strSQL = "select TM," + ColunmName + " from T_WSD_DATA where TM>=to_date('" + sDT + "','yyyy/MM/dd hh24:mi:ss') and TM<to_date('" + DateTime.Parse(eDT).AddDays(1).ToString("yyyy-MM-dd") + "','yyyy/MM/dd hh24:mi:ss') and DEVICEID='" + deviceId + "' order by tm";
  286. DataSet ds = odm.getsql(strSQL);
  287. DataSet ds2 = odm.getsql("select wdmin,wdmax,sdmin,sdmax from t_wsd_attr where deviceid='" + deviceId + "'");
  288. string attr1 = "";
  289. string attr2 = "";
  290. if (ds2.Tables[0].Rows.Count > 0)
  291. {
  292. switch (ColunmName.ToUpper())
  293. {
  294. case "WENDU":
  295. attr1 = ds2.Tables[0].Rows[0]["wdmax"].ToString();
  296. attr2 = ds2.Tables[0].Rows[0]["wdmin"].ToString();
  297. this.valueAxis_title1.Value = "最大报警阀值";
  298. this.valueAxis_title2.Value = "最小报警阀值";
  299. break;
  300. case "SHIDU":
  301. attr1 = ds2.Tables[0].Rows[0]["sdmax"].ToString();
  302. attr2 = ds2.Tables[0].Rows[0]["sdmin"].ToString();
  303. this.valueAxis_title1.Value = "最大报警阀值";
  304. this.valueAxis_title2.Value = "最小报警阀值";
  305. break;
  306. default:
  307. attr1 = ds2.Tables[0].Rows[0]["wdmax"].ToString();
  308. attr2 = ds2.Tables[0].Rows[0]["wdmin"].ToString();
  309. this.valueAxis_title1.Value = "最大报警阀值";
  310. this.valueAxis_title2.Value = "最小报警阀值";
  311. break;
  312. }
  313. }
  314. if (ds.Tables[0].Rows.Count > 0)
  315. {
  316. foreach (DataRowView drv in ds.Tables[0].DefaultView)
  317. {
  318. sb.Append("{" + "date:\"" + DateTime.Parse(drv["TM"].ToString()).ToString("yyyy/MM/dd HH:mm") + "\""
  319. + (drv[ColunmName] != null ? (",W:" + double.Parse(drv[ColunmName].ToString()).ToString("N2").Replace(",", "")) : "")
  320. + (attr1 != "" ? (",max:" + attr1) : "")
  321. + (attr2 != "" ? (",min:" + attr2) : "")
  322. + "},");
  323. }
  324. /*********************************温度最值********************************/
  325. //LINQ取获得数据中的最大值和最小值
  326. var lists = (from row in ds.Tables[0].AsEnumerable()
  327. orderby row.Field<decimal>(ColunmName) descending
  328. select row);
  329. DataRow drv_max = lists.FirstOrDefault();
  330. DataRow drv_min = lists.LastOrDefault();
  331. data = sb.ToString().TrimEnd(',');
  332. data = "[" + data + "]";
  333. //string max = (double.Parse(drv_max[ColunmName].ToString()) > double.Parse(attr1) ? double.Parse(drv_max[ColunmName].ToString()).ToString() : double.Parse(attr1).ToString());
  334. //为隐藏域赋值
  335. this.ss.Value = data;
  336. //this.wd_max.Value = ((double.Parse(max) * 1.001)).ToString();
  337. }
  338. else
  339. {
  340. progressBar.Style["display"] = "none";
  341. }
  342. this.ss_max.Value = attr1;
  343. this.ss_min.Value = attr2;
  344. //标题赋值
  345. //this.lblTitle.Text = deviceName + "UPS曲线";
  346. this.lblTitle.Text = this.valueAxis_title.Value + "曲线";
  347. //为图表主副标题赋值
  348. this.lbl_data.Text = "[" + deviceName + "]" + "-" + this.valueAxis_title.Value + "曲线图";
  349. this.lbl_subtitle.Text = "时间自" + DateTime.Parse(sDT).ToString("yyyy年MM月dd日") + "至" + DateTime.Parse(eDT).ToString("yyyy年MM月dd日");
  350. }
  351. /// <summary>
  352. /// 查询按钮事件
  353. /// </summary>
  354. /// <param name="sender"></param>
  355. /// <param name="e"></param>
  356. protected void btnQuery_Click(object sender, EventArgs e)
  357. {
  358. string bTM = this.bTM.Value.Trim(); //开始时间
  359. string eTM = this.eTM.Value.Trim(); //结束时间
  360. string deviceId = this.ddlSiteNM.SelectedItem.Value.Trim(); //设备编码
  361. string deviceName = this.ddlSiteNM.SelectedItem.Text.Trim(); //设备名称
  362. SetChartData(deviceId, deviceName, bTM, eTM, ViewState["ColumnName"].ToString());
  363. }
  364. /// <summary>
  365. /// 绑定下拉框数据
  366. /// </summary>
  367. public void BindDropDownList()
  368. {
  369. string strSQL2 = "select DEVICEID,DEVICENAME from T_DEVICE_LIST where DEVICEID in (SELECT DISTINCT DEVICEID FROM T_WSD_Data)";
  370. DataSet ds2 = odm.getsql(strSQL2);
  371. this.ddlSiteNM.DataSource = ds2;
  372. this.ddlSiteNM.DataTextField = "DEVICENAME";
  373. this.ddlSiteNM.DataValueField = "DEVICEID";
  374. this.ddlSiteNM.DataBind();
  375. }
  376. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/114712
推荐阅读
相关标签
  

闽ICP备14008679号