Jqplot的应用
发布时间:2013-12-17 05:38:01来源:阅读(994)
在自己做一个MVC的小项目时,需要用图表来显示统计信息,看了很多JS图表,有HighCharts、JqPlot、JsChart,其中HighCharts和JsChart只能用作个人使用、为了以后项目中能使用,在这用了JqPlot(个人和商业都免费),先学习下。
关于JqPlot的下载可以去:www.jqplot.com/
关于JqPlot的详细中文配置参考:http://blog.csdn.net/gaoyusi4964238/article/details/4378459
下面开始制作一个柱状图:
1.引用JS,Jquery必须引用
- <link href="@Url.Content("~/Content/Jqplot/jquery.jqplot.css")" rel="stylesheet" type="text/css" />
- <script src="@Url.Content("~/Scripts/Jqplot/jquery.jqplot.min.js")" type="text/javascript">script>
- <script src="@Url.Content("~/Scripts/Jqplot/jqplot.barRenderer.min.js")" type="text/javascript">script>
- <script src="@Url.Content("~/Scripts/Jqplot/jqplot.categoryAxisRenderer.min.js")" type="text/javascript">script>
- <script src="@Url.Content("~/Scripts/Jqplot/jqplot.pointLabels.min.js")" type="text/javascript">script>
- <script src="@Url.Content("~/Scripts/Jqplot/jqplot.pieRenderer.min.js")" type="text/javascript">script>
- <script src="@Url.Content("~/Scripts/JS/UserConsumeDay.js")" type="text/javascript">script>
-
说明:最后一个引用是为了使用IE7/8,
2.在HTML中敲入代码
- <div id="chart1" style="width: 600px; height: 250px;float:left;">
- div>
3.编写JS代码
- $.ajax({
- type: "GET",
- dataType: "json",
- url: "/UserConsumeDay/IndexView/",
- data: "id=" + userId,
- success: function (msg) {
- var dataValue = new Array();
- var ticks = new Array();
- $(msg).each(function (index, value) {
- dataValue.push(msg[index].Consume);
- ticks.push(msg[index].YearMonth);
- });
- $("#chart1").html("");
- var plot1 = $.jqplot('chart1', [dataValue], {
- animate: !$.jqplot.use_excanvas,
- seriesDefaults: {
- renderer: $.jqplot.BarRenderer,
- pointLabels: { show: true }
- },
- title: $("#User").find("option:selected").text() + '--月度消费统计表( Last 12 Monthes)',
- axes: {
- xaxis: {
- renderer: $.jqplot.CategoryAxisRenderer,
- ticks: ticks
- },
- yaxis: {
- pad: 1.05,
- tickOptions: { formatString: '$%d'}
- }
- }
- });
- }
- });
在这我用了Jquery的Ajax获取数据(msg为json数据),并将其转化为横坐标和纵坐标的数组。
4.效果图
5.编码过程中遇到的问题
- select sum(u.ConsumeMoney) as totalMoney, convert(nvarchar(7), u.ConsumeDay,120) as monthConsume
- from T_Consume u where DATEDIFF(MONTH,u.ConsumeDay,GETDATE())<12
- group by convert(nvarchar(7), u.ConsumeDay,120)