Making Graphs
Contents
Introduction
Expected Time: 30 min
In this tutorial you will learn how to create several graphs that can help you presenting your work. First we will focus on a type of graph related to a world map. After that, some methods for one-dimensional and two-dimensional graphs will be discussed.
World Map
Before you start with your Grasshopper script, please download the following Rhino file as your basis: Download Rhino World Map
Location pattern
In this chapter, we will create a hexagonal pattern for a specific country. First add a curve parameter to your canvas and set a specific country on the Grasshopper parameter. In our case, we selected Brazil.
- Add a Curve Parameter to the canvas
- Set the Country Boundary on the curve parameter
Create a surface from the boundary of the country.
- Create a boundary surface
Now we need to find the plane of the surface. This can be done by evaluating the box properties of the curve.
- Find the plane of the Boundary Surface using the Evaluate Box
- Set the U, V and W parameter to 0
Add a pattern to the canvas. These patterns can be found in the
tab. In this tutorial, the hexagonal pattern is used.- Create a hexagonal pattern from the plane
- Add Number Sliders to the Size, Extend X and Extend Y inputs to fill the surface
The hexagons will now be projected on the surface. Since the hexagons are created individually, the input of the projection needs to be flattened.
- Project the Hexagon pattern on the surface
- Flatten the Curve input
As you can see, this created a hexagon pattern in the country region. Don't forget to turn off the preview of all the nodes you don't need.
If you now want to get one specific hexagon, add a List item to the Hexagon Cells output. Make sure you flatten the data.
- Connect a List Item to the Cells output
- Add a Number Slider to the Index input
- Flatten the List input of the List Item
To finalize your script, add two Curve parameters to the results, and change the name.
- Add two Curve Parameters which hold the result
- Rename the Curve Parameters
Basic 1D Graph
In this chapter you will learn how to create a basic 1D graph. The data will be based on two random sets of numbers: the height and the volume of each point.
First create a range of numbers, this will define the amount of data points in the graph.
- Add a range component to the canvas
- Define the size of the range with a Number Slider
Now we construct a vector that holds the location of each point. For the Z-component, we use the random component. The amount of random numbers is equal to the amount of data points. Furthermore, we define a domain with the maximum and minimum possible value for the Z component.
- Construct a vector using the range as X-component and a random node as Z-component
- Add a Random node for the Z-component input
- Define a domain with of the Random component
Create a sphere that represents the data point. The radius is defined by a random number, again with a domain that defines the extremes. Use the same Number Slider for the amount of values.
- Add a Sphere component to the canvas
- Define the radius of the sphere with a Random node
- Define the domain of the Random node with a Construct Domain node
- Connect the Number slider of the Range node to the Number input of the Random node
Now we move the spheres to the correct location using the move component. We also need the volume and centroid of each sphere.
- Add a move component to the canvas. Use the created Vector XYZ as Motion input
- Calculate the centroids with the Volume function
Add a gradient to the canvas. The color will represent the size of the spheres.
- The gradient node can be found at
- Right-click on the gradient and select a preset you like. The color on the right will represent the maximum. The color on the left the minimum.
As you can see, the gradient asks for a Lower limit, Upper limit and Parameter. In our case, the lower limit can be left like this, because it already holds the value 0 (represents volume = 0). For the Upper limit we need the maximum. Sort the list of volumes and reverse it.
- Sort the volume list
- Reverse the Keys output
Use a list item to get the first (maximum) item from the list. Connect it to the upper limit. Connect the Volume output of the Volume component to the Parameter input of the gradient.
- Find the first item with the list item component. Keep the Index input as it is.
- Connect the List Item output to the upper limit
- Connect the original Volume output to the Parameter input
A line between the points can be created by adding a polyline to the Centroid output of the Volume component.
- Create a Polyline from the centroid output
Now we need to display the Data Points. This can be achieved with the Custom Preview and material component. The diffuse input of the material will be based on the gradient.
- Display the original geometry with the Custom Preview component
- Create a material for using the Gradient as Diffuse. Connect the material to the Material input
If you want to make an x-axis with the values for each Data Point, construct a point from the original Range component we began the script wit. Using the Text Tag, you can display the values.
- Construct points from the original Range component.
- Visualize the x-components with the Text Tag. Use the created points as Location input
- Connect the Range output to the Text input of the Text Tag
Basic 2D Graph
Here you will learn how to create a two-dimensional graph, based on two axes.
Add a Series component to the canvas. This component will hold the values for the x-axis and the step size. Change the Count input to 12 and the Step input to 10.
- Add a Series component to the canvas
- Change the Count input to 12
- Change the Step input to 10
Construct points from the created number sequence. After that, create a cylinder from each point. This cylinder geometries can be capped to make them closed.
- Construct points from the Series. Use the Series output for the X coordinate input.
- Create a cylinder with the point as Base input.
- Cap the cylinders
Create a range of random numbers for the y-values of the cylinders. The amount of numbers can be based on the length of the series component.
- Define a random length with the Random component
- Use the length of the Series component for the Number input
Add a text parameter to the canvas. By setting multiple texts, you can add all the months of the year.
- Add a text parameter
- Write down the names of the months in the Text parameter
Create a Text tag with the original points as location input. By adding a XZ plane in between, the tags will be positioned in the right direction. Change the size and colour of the text to your wish.
- Add a Text Tag to the canvas
- Connect the Text Parameter to the Text input.
- Add a XZ Plane between the Point output and the Location input
Additional Scripts
In the following downloadable zip-file, you will find some additional scripts that may be useful for your projects. Don't forget to Credit the Author.
- Downloadable file: Download Additional Scripts