Bubble Charts are used to visualize data in 3 dimensions. It is very similar to Scatter Chart except that size of bubble represents another parameter. Out of the three parameters required (x, y, z), x & y determine the bubble's position on X & Y Axis whereas z determines its size. Given example shows simple ASP.NET MVC Bubble Chart along with source code that you can try running locally.
@{
Layout = null;
}
<!DOCTYPE HTML>
<html>
<head>
<script>
window.onload = function () {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
exportEnabled: true,
theme: "dark2", // "light1", "light2", "dark1", "dark2"
title: {
text: "Surface Temperature vs Size & Distance from Sun"
},
axisX: {
title: "Distance From Sun (in Million Miles)",
logarithmic: true,
maximum: 4000
},
axisY: {
title: "Surface Temperature (in Kelvin)"
},
data: [{
type: "bubble",
indexLabel: "{label}",
toolTipContent: "{label}:<br>Distance From Sun: {x}mn miles<br>Avg. Surface Temp: {y} Kelvin<br>Diameter: {z} miles",
dataPoints: @Html.Raw(ViewBag.DataPoints)
}]
});
chart.render();
}
</script>
</head>
<body>
<div id="chartContainer" style="height: 370px; width: 100%;"></div>
<script type="text/javascript" src="https://cdn.canvasjs.com/canvasjs.min.js"></script>
</body>
</html>
using ASPNET_MVC_ChartsDemo.Models;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Web.Mvc;
namespace ASPNET_MVC_ChartsDemo.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
List<DataPoint> dataPoints = new List<DataPoint>();
dataPoints.Add(new DataPoint("Mercury", 36, 452, 3031));
dataPoints.Add(new DataPoint("Venus", 67.2, 726, 7521));
dataPoints.Add(new DataPoint("Earth", 93, 285, 7926));
dataPoints.Add(new DataPoint("Mars", 141.6, 230, 4222));
dataPoints.Add(new DataPoint("Jupiter", 483.6, 120, 88729));
dataPoints.Add(new DataPoint("Saturn", 886.7, 88, 74600));
dataPoints.Add(new DataPoint("Uranus", 1784, 59, 32600));
dataPoints.Add(new DataPoint("Neptune", 2794.4, 48, 30200));
ViewBag.DataPoints = JsonConvert.SerializeObject(dataPoints);
return View();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
namespace ASPNET_MVC_ChartsDemo.Models
{
//DataContract for Serializing Data - required to serve in JSON format
[DataContract]
public class DataPoint
{
public DataPoint(string label, double x, double y, double z)
{
this.Label = label;
this.X = x;
this.Y = y;
this.Z = z;
}
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "label")]
public string Label = "";
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "x")]
public Nullable<double> X = null;
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "y")]
public Nullable<double> Y = null;
//Explicitly setting the name to be used while serializing to JSON.
[DataMember(Name = "z")]
public Nullable<double> Z = null;
}
}
Setting zoomEnabled property to true enables the zooming / panning feature. The axis of zoom / pan can also be selected using zoomType property. Other commonly used customization options are animationEnabled, theme, etc.
Note For step by step instructions, follow our ASP.NET MVC Integration Tutorial