Peity is a minimalist jQuery plugin that allows you to create small, inline charts within your web pages. It’s designed for creating simple, visually appealing charts without the overhead of large JavaScript libraries like Chart.js or D3.js. Peity excels at providing quick, easily-integrated data visualizations directly within the text flow of your webpage. It supports several chart types, including line charts, bar charts, and pie charts, making it versatile for various data representation needs.
Peity’s strengths lie in its simplicity and ease of use. Here are some key reasons to choose it:
Peity is readily available via a CDN or you can download it directly. The simplest method is to include it via a CDN link in your HTML file’s <head>
section:
<script src="https://cdn.jsdelivr.net/npm/peity"></script>
Alternatively, download the jquery.peity.min.js
file and include it locally:
<script src="path/to/jquery.peity.min.js"></script>
Remember that Peity requires jQuery. Ensure you’ve included jQuery before including the Peity script. For example:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/peity"></script>
Peity’s core functionality is extremely simple. You create a chart by selecting an HTML element containing your data and calling the .peity()
method. The data is typically provided as a space-separated string of numbers.
<span class="peity-line" data-peity='{ "fill": ["#008000"], "stroke": "#008000" }'>1 2 3 4 5 2 1</span>
<script>
$('.peity-line').peity('line');
</script>
This code snippet creates a simple line chart. The data-peity
attribute allows for optional configuration options (in JSON format), in this case we’ve specified fill and stroke color. The JavaScript code then initializes the chart using the appropriate Peity method (line
in this example). Other chart types like bar
, pie
, etc., can be used similarly, by replacing "line"
with the desired type. More advanced usage and options are detailed in the following sections.
Line charts in Peity display data as a connected series of points, ideal for showing trends over time or illustrating relationships between data points.
Basic Usage:
<span class="line-chart">5,3,9,6,5,9,7,3,5,2</span>
<script>
$('.line-chart').peity('line');
</script>
This creates a basic line chart. Data values are comma-separated.
Options:
fill
: (boolean or array of colors) Fills the area under the line. false
for no fill. An array allows for multiple colors to create gradients.stroke
: (color) Sets the line color.strokeWidth
: (number) Specifies the line width.height
: (number) Sets the chart height.width
: (number) Sets the chart width.max
: (number) Sets the maximum value on the y-axis.min
: (number) Sets the minimum value on the y-axis.Bar charts in Peity represent data as a series of vertical bars, useful for comparing the values of different categories.
Basic Usage:
<span class="bar-chart">5,3,9,6,5,9,7,3,5,2</span>
<script>
$('.bar-chart').peity('bar');
</script>
This creates a basic bar chart.
Options:
fill
: (array of colors) Sets the color of each bar. If fewer colors are provided than bars, they will be cycled.height
: (number) Sets the chart height.width
: (number) Sets the chart width.max
: (number) Sets the maximum value on the y-axis.min
: (number) Sets the minimum value on the y-axis.Pie charts in Peity display data as proportional slices of a circle, showing the relative contribution of each part to a whole.
Basic Usage:
<span class="pie-chart">1,2,3,4</span>
<script>
$('.pie-chart').peity('pie');
</script>
This creates a basic pie chart.
Options:
fill
: (array of colors) Sets the color of each slice. If fewer colors are provided than slices, they will be cycled.radius
: (number) Sets the radius of the pie chart.innerRadius
: (number) For donut charts (see below).Donut charts are similar to pie charts but with a hole in the center, often used to emphasize the proportion rather than the absolute values. Donut charts are created using the pie
chart type with the innerRadius
option.
Basic Usage:
<span class="donut-chart">1,5,9,3</span>
<script>
$('.donut-chart').peity('pie', {innerRadius: .5});
</script>
This creates a donut chart with a 50% inner radius. Adjust the innerRadius
value (between 0 and 1) to control the size of the hole. All other options for pie charts apply to donut charts as well.
Peity primarily accepts data in two formats: space-separated strings and arrays/objects.
Space-separated strings: This is the simplest format, where numerical data values are separated by spaces within the HTML element’s text content. This is the method shown in most basic examples.
<span>1 2 3 4 5</span>
Arrays/Objects: For more complex scenarios or when using options that require associating data points with additional attributes (e.g., labels or colors), you can provide data as a JavaScript array or object within the data
option of the peity
function.
$('.my-chart').peity('line', {
data: [1, 2, 3, 4, 5],
//other options here...
; })
Or using an object:
$('.my-chart').peity('bar', {
data: [{ value: 1, label: 'A'}, {value: 2, label: 'B'}, {value: 3, label: 'C'}],
//other options here...
; })
Note that when using an object, you must specify a value
property for the numerical data. Peity will currently ignore other properties, although future versions may leverage this for additional features.
Peity primarily utilizes the data-peity
attribute for specifying options. This attribute should contain a JSON object defining the chart’s configuration:
<span data-peity='{"fill": ["#f00", "#0f0"], "stroke": "#00f"}'>1 2 3 4 5</span>
This will create a line chart with red and green fills and a blue stroke. Note that options set within the data-peity
attribute will override options set in the javascript call.
Peity is not optimized for extremely large datasets. For very large amounts of data, consider using a more robust charting library like Chart.js or D3.js, which are designed for performance with many data points. Peity’s intended use is for smaller, inline charts where ease of use and minimal code are prioritized over handling massive datasets.
To update the data in a Peity chart, you must destroy the existing chart and then re-create it with the new data. Peity doesn’t offer a built-in method for updating data in place.
let myChart = $('.my-chart');
.peity('destroy'); //remove existing chart
myChart
let newData = [6, 7, 8, 9, 10];
.peity('line', {data: newData}); //recreate chart with new data myChart
This ensures that the chart is correctly redrawn with the updated values. Remember to replace 'line'
with the appropriate chart type.
You can control the width and height of Peity charts using the width
and height
options. These options accept numerical values (e.g., width: 100
, height: 50
) representing pixels. You can also specify these values as percentages or other valid CSS units, but this is less common. These are passed either via the data-peity
attribute or as part of the options object when calling .peity()
.
$('.my-chart').peity('line', { width: 200, height: 100 });
or
<span data-peity='{ "width": 150, "height": 60 }'>1,2,3,4,5</span>
Colors are primarily controlled using the fill
option. For line charts, fill
can be a boolean (true for filling the area under the line, false otherwise), a single color string (e.g., “#ff0000” or “red”), or an array of color strings for gradient fills. For bar and pie charts, fill
is an array of colors, one for each bar or slice.
$('.my-chart').peity('bar', { fill: ['#008000', '#000080', '#800000'] });
The appearance of lines in line charts is controlled by the stroke
and strokeWidth
options. stroke
sets the line color (similar to fill
), and strokeWidth
sets its thickness in pixels.
$('.my-chart').peity('line', { stroke: '#0000FF', strokeWidth: 3 });
Peity doesn’t directly support gridlines or markers. This is a deliberate design choice to maintain its minimalist nature. To achieve this functionality, you will likely need to utilize CSS or another charting library.
Peity doesn’t have built-in support for labels or legends. To add labels, you’ll need to add them manually within your HTML alongside the charts. For more advanced legend features, you would need to use another library.
Peity itself does not provide tooltips or other interactive elements. To add interactivity (hover effects, tooltips, etc.), you’ll need to use separate JavaScript libraries or write custom JavaScript code that interacts with the chart elements.
Customization options often vary slightly depending on the chart type. For example, the innerRadius
option is specific to donut charts (a type of pie chart). Consult the individual chart type descriptions (above) for their specific customization options. Refer to the complete list of options available within the Peity source code for a comprehensive overview. Remember, that many advanced customization needs are better suited to more feature-rich libraries. Peity’s power is in its simplicity and ease of integration.
Peity is a jQuery plugin, so its direct integration with modern JavaScript frameworks like React, Angular, and Vue requires a bit of care. The simplest approach involves using Peity within a lifecycle method (e.g., componentDidMount
in React, ngOnInit
in Angular) after the DOM element has been rendered.
React Example:
import React, { useEffect, useRef } from 'react';
import $ from 'jquery';
import 'peity'; //Import peity
function MyComponent() {
const chartRef = useRef(null);
useEffect(() => {
if (chartRef.current) {
$(chartRef.current).peity('line');
}, []);
}
return (
<div>
<span ref={chartRef}>5,3,9,6,5,9,7,3,5,2</span>
</div>
;
)
}export default MyComponent;
Remember to include jQuery and Peity in your project, and adjust the code according to your chosen framework’s component lifecycle. For larger applications consider a wrapper component to manage chart creation and updates more cleanly.
Peity doesn’t directly support creating custom chart types. Its core functionality is limited to the predefined types. To create a completely new chart type, you would need to write a separate JavaScript plugin or use a different charting library.
Peity can be used alongside other JavaScript libraries, but careful consideration is needed to avoid conflicts. Ensure that all libraries are included correctly and that there are no naming conflicts or dependencies that could cause issues. Generally, if the other libraries don’t directly manipulate the DOM elements used by Peity, integration should be straightforward.
For optimal performance with Peity:
peity('destroy')
method before recreating a chart.Uncaught TypeError: $(...).peity is not a function
: This usually means that either jQuery or the Peity library hasn’t been included correctly in your HTML file. Double-check that both <script>
tags are present and the paths are accurate. Make sure jQuery is included before Peity.
Charts not rendering: This can be due to several reasons: incorrect data format (e.g., commas instead of spaces), incorrect options in the data-peity
attribute or options object, or conflicts with other JavaScript libraries. Inspect the browser’s console for JavaScript errors. Ensure that the selector used with .peity()
actually matches elements in your HTML.
Incorrect chart appearance: If the chart looks wrong (e.g., wrong colors, incorrect size), double-check the values and types of your options in the data-peity
attribute or the options object. Pay close attention to the data format.
Browser’s developer console: Use your browser’s developer console (usually opened with F12) to check for JavaScript errors and warnings. These messages often pinpoint the exact location of problems in your code.
Simplify your code: If you’re having trouble, create a minimal, reproducible example. Try removing unnecessary options or features to isolate the problem.
Inspect the DOM: Use your browser’s developer tools to inspect the HTML elements where you’re trying to create charts. Ensure the elements exist and the data is correctly inserted.
Check jQuery version: Peity relies on jQuery. Ensure you are using a compatible version of jQuery. Very old or very new versions might cause unexpected issues.
Peity generally works well in modern browsers. However, very old or outdated browsers might not support all its features or may have rendering issues. Peity is designed to work with jQuery, so browser compatibility is also subject to the limits of the jQuery library. For best results, support only widely-used, modern browsers.
No built-in support for accessibility: Peity itself doesn’t include features like ARIA attributes for accessibility. For better accessibility, you’ll need to add appropriate ARIA attributes manually.
Limited interaction: Peity provides no built-in interaction features such as tooltips or hover effects. You’ll have to implement these using other libraries.
Large datasets: Peity is not optimized for extremely large datasets. For large datasets, use alternative libraries designed for handling large amounts of data.
Peity options are passed as a JSON object either through the data-peity
attribute or as the second argument to the .peity()
method. Many options are applicable across multiple chart types, but some are specific to certain chart types. Options set in the data-peity
attribute will override those passed to .peity()
.
Common Options:
data
: (Array or String) The data to be charted. If a string, values should be space-separated. If an array, values should be numbers (or objects with a value
property).width
: (Number) The width of the chart in pixels.height
: (Number) The height of the chart in pixels.fill
: (Boolean, String, or Array) Fill color(s). For line charts, a boolean indicates whether to fill the area under the line. For bar and pie charts, it’s an array of colors.stroke
: (String) Stroke color for lines.strokeWidth
: (Number) Stroke width for lines.min
: (Number) Minimum value for the y-axis (bar and line charts).max
: (Number) Maximum value for the y-axis (bar and line charts).Pie Chart Specific Options:
innerRadius
: (Number, 0-1) The radius of the inner circle for donut charts (pie charts with a hole). 0 is a full pie, 1 is a line.Refer to the previous sections (“Chart Types” and “Customization Options”) for more detailed explanations of these options and chart-specific options.
.peity(type, options)
: This is the primary method to initialize a Peity chart. type
is a string representing the chart type (“line”, “bar”, “pie”). options
is an optional JSON object specifying chart options.
.peity('destroy')
: This method removes a Peity chart from the DOM. This is essential when updating the chart’s data.
There are no other public methods directly exposed by the Peity plugin. Additional functionality needs to be implemented via custom JavaScript.
Peity does not trigger any custom events. To add interactivity, you’ll need to use standard DOM events (like mouseover
, mouseout
, click
) on the chart’s container element and handle them in your own JavaScript code. Note that the chart element’s contents are typically generated dynamically by Peity; it’s usually better to handle events on the parent element rather than directly on the dynamically generated elements.