// # C3 Scatter Plot // _This example covers a scatter plot using different C3 concepts as discussed in the // API Overview._ // ## Student Data // An `interface` is just for TypeScript type annotation and can be ignored for JavaScript. interface StudentData { name: string id: number gender: string age: number grade: number } var student_id = 0; // Create **student_data** _array_. var student_data = [ { name: "Joe", id: ++student_id, gender: "male", age: 16, grade: 3.2 }, { name: "Lisa", id: ++student_id, gender: "female", age: 18, grade: 2.3 }, { name: "Patrick", id: ++student_id, gender: "male", age: 28, grade: 1.4 }, { name: "Mandy", id: ++student_id, gender: "female", age: 32, grade: 3.8 }, { name: "Tommy", id: ++student_id, gender: "male", age: 9, grade: 1.7 }, ]; // ## Scatter Plot // Create the **scatter plot** and attach to the DOM. var my_chart = new c3.Plot({ anchor: '#scatter_plot_example', height: 300, width: '50%', // ### Scales // Setup the **scales** for student ages 0-50 and grades 0-4. h: d3.scale.linear().domain([0, 50]), v: d3.scale.linear().domain([0, 4]), // ### Axes // Add **margins** and disable **cropping** so the dots can extend past the edge of the plot. margins: 10, crop_margins: false, // Add **axes** for grid lines and labels axes: [ new c3.Axis.X({ grid: true, label: "Age", }), // The y axis will display letter grades new c3.Axis.Y({ grid: true, label: "Grade", tick_values: [0, 1, 2, 3, 4], // Create a **quantize scale** to translate from numeric to letter grades. tick_label: d3.scale.quantize() .domain([0, 4]) .range(['F', 'D', 'C', 'B', 'A']), }), ], // ### Layers // Setup the plot **layers** layers: [ // Create a `c3.Plot.Layer.Scatter` **scatter layer** new c3.Plot.Layer.Scatter({ // ### Data set accessors // Bind with student **data** data: student_data, // **key** accessor to assign unique id. // This aids in animations when students are added and removed. key: (student) => student.id, // Accessors for **x** and **y** positions based on the age and grade of the students. x: (student) => student.age, y: (student) => student.grade, // All dots have a **radius** of 10 r: 10, // **Filter** data for underage students filter: (student) => student.age >= 16, // ## Selection Options // Enable **animation** in the chart when updating data point_options: { animate: true, duration: 1000, }, // Enable **labels** label_options: { text: (student) => student.grade.toFixed(1), styles: { 'fill': (student) => student.gender === 'male' ? "white" : "black", 'font-size': "x-small", }, }, circle_options: { // ## Styles // Set CSS **classes** based on the data. There is a corresponding CSS rule in // the `