### Install TTY::Sparkline Gem Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md To use TTY::Sparkline, add it to your application's Gemfile and run bundle install, or install it directly using the gem install command. ```ruby gem "tty-sparkline" # Then execute: # $ bundle install # Or install it yourself as: # $ gem install tty-sparkline ``` -------------------------------- ### Install TTY::Sparkline Gem Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Instructions for installing the TTY::Sparkline gem, either by adding it to a Gemfile or installing it directly using the gem command. ```ruby gem "tty-sparkline" # Or install directly # $ gem install tty-sparkline ``` -------------------------------- ### Render Sparkline with Custom Formatting Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md The render method accepts a block that allows custom formatting of each bar based on its value, character, column, and row. This example adds a space between bars. ```ruby sparkline = TTY::Sparkline.new(1..8) puts sparkline.render do |val, bar, col, row| col == 7 ? bar : "#{bar} " end ``` -------------------------------- ### Create and Render a Basic Sparkline Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Instantiate TTY::Sparkline with data and call the render method to generate the sparkline string. Print the result to display it in the terminal. ```ruby sparkline = TTY::Sparkline.new(1..8) puts sparkline.render # => "▁▂▃▄▅▆▇█" ``` -------------------------------- ### Create TTY::Sparkline Instance with Data Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Demonstrates creating a TTY::Sparkline instance using the `TTY::Sparkline.new` constructor with different data formats like ranges, arrays, or by appending data incrementally. The `render` method is used to display the sparkline. ```ruby require "tty-sparkline" # Create with a range of datasparkline = TTY::Sparkline.new(1..8) puts sparkline.render # => ▁▂▃▄▅▆▇█ # Create with an arraysparkline = TTY::Sparkline.new([10, 20, 30, 25, 15, 35, 40, 30]) puts sparkline.render # => ▁▃▆▄▂▇█▆ # Create empty and add data latersparkline = TTY::Sparkline.newsparkline << 1 << 2 << 3 << 4 puts sparkline.render # => ▁▃▅█ ``` -------------------------------- ### Initialize Sparkline with Data Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md A new sparkline instance can be created with initial data provided as an Array or Range. Alternatively, data can be appended later. ```ruby sparkline = TTY::Sparkline.new(1..8) sparkline = TTY::Sparkline.new([1, 2, 3, 4, 5, 6, 7, 8]) ``` -------------------------------- ### Configuring Sparkline Width Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Explains how to limit the number of columns a sparkline occupies using the `:width` keyword. This is useful for controlling the horizontal space the sparkline takes in the terminal, especially when combined with height. ```ruby TTY::Sparkline.new(1..8, width: 5) ``` ```ruby print sparkline.render ``` ```ruby TTY::Sparkline.new(1..8, height: 3, width: 5) ``` -------------------------------- ### Configuring Sparkline Minimum Value Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Demonstrates how to set a custom minimum value for scaling sparkline bars using the `:min` keyword. This overrides the default behavior of calculating the minimum from the data set, allowing for better visibility of lower values. ```ruby sparkline = TTY::Sparkline.new([100, 75, 100, 50, 80]) ``` ```ruby sparkline = TTY::Sparkline.new([100, 75, 100, 50, 80], min: 0) ``` -------------------------------- ### Basic Sparkline Rendering with TTY-Sparkline Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Demonstrates basic sparkline rendering with TTY-Sparkline, including setting minimum and maximum values to control proportionality and clamping. ```ruby require "tty-sparkline" # With min: 0, values are more proportionalsparkline = TTY::Sparkline.new([100, 75, 100, 50, 80], min: 0) puts sparkline.render # => █▆█▄▆ # Clamping with max - values above max show as full barssparkline = TTY::Sparkline.new([100, 75, 300, 50, 80], max: 100) puts sparkline.render # => █▄█▁▅ # Can also set via attributessparkline = TTY::Sparkline.new(1..8)sparkline.min = 3sparkline.max = 6 puts sparkline.render # => ▁▁▁▃▅███ ``` -------------------------------- ### Configuring Sparkline Top Position Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Demonstrates how to set the vertical position of a sparkline using the `:top` keyword. This allows the sparkline to be placed a specific number of lines from the top of the terminal screen. It also shows dynamic positioning using tty-screen. ```ruby TTY::Sparkline.new(top: 10) ``` ```ruby TTY::Sparkline.new(top: TTY::Screen.height / 2) ``` -------------------------------- ### Configuring Sparkline Left Position Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Illustrates how to set the horizontal position of a sparkline using the `:left` keyword. This enables positioning the sparkline a specified number of columns from the left edge of the terminal window. Dynamic positioning with tty-screen is also shown. ```ruby TTY::Sparkline.new(left: 50) ``` ```ruby TTY::Sparkline.new(left: TTY::Screen.width / 2) ``` -------------------------------- ### Configuring Sparkline Bar Characters Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Shows how to customize the characters used to render sparkline bars with the `:bars` keyword. This allows for using a different set of characters to represent the data visually, offering more stylistic control. ```ruby sparkline = TTY::Sparkline.new(1..8, bars: %w[_ - = ^]) ``` ```ruby sparkline.render ``` -------------------------------- ### Render TTY::Sparkline with Customization Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Shows how to render a TTY::Sparkline chart using the `render` method. It covers basic rendering, overriding scaling with `min` and `max` parameters, and customizing individual bar output using a block, such as adding spaces or colors. ```ruby require "tty-sparkline" sparkline = TTY::Sparkline.new(1..8) # Basic render puts sparkline.render # => ▁▂▃▄▅▆▇█ # Render with custom min/max scaling puts sparkline.render(min: 3, max: 6) # => ▁▁▁▃▅███ # Render with block for customization (e.g., adding spaces) output = sparkline.render do |val, bar, col, row| col == 7 ? bar : "#{bar} " end puts output # => ▁ ▂ ▃ ▄ ▅ ▆ ▇ █ ``` -------------------------------- ### Configuring Sparkline Maximum Value Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Explains how to set a custom maximum value for scaling sparkline bars using the `:max` keyword. This allows for controlling the upper limit of the scale, making lower values more prominent when the data contains very high outliers. ```ruby sparkline = TTY::Sparkline.new([100, 75, 300, 50, 80]) ``` ```ruby sparkline = TTY::Sparkline.new([100, 75, 300, 50, 80], max: 100) ``` -------------------------------- ### Append Data to Sparkline Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Illustrates how to add new data points to an existing TTY::Sparkline instance using the `push`, `append`, and `<<` methods. These methods are aliases and support method chaining, which is useful for streaming data. ```ruby require "tty-sparkline" sparkline = TTY::Sparkline.new # Using push with single or multiple valuessparkline.push(1).push(2)sparkline.push(3, 4) # Using appendsparkline.append(5).append(6) # Using << operatorsparkline << 7 << 8 puts sparkline.render # => ▁▂▃▄▅▆▇█ puts sparkline.size # => 8 ``` -------------------------------- ### Configure Sparkline Position and Size Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Sparkline charts can be positioned using 'top' and 'left' options, and their dimensions can be controlled with 'height' and 'width' options during initialization. ```ruby sparkline = TTY::Sparkline.new(1..8, top: 10, left: 50)sparkline = TTY::Sparkline.new(1..8, height: 5, width: 100) ``` -------------------------------- ### Configure TTY::Sparkline Height Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Demonstrates configuring the height of a TTY::Sparkline chart using the `:height` option. A height greater than 1 provides better visual resolution for data variations, rendering the sparkline across multiple terminal lines. ```ruby require "tty-sparkline" # Single line (default)sparkline = TTY::Sparkline.new(1..8) puts sparkline.render # => ▁▂▃▄▅▆▇█ # 3 lines highsparkline = TTY::Sparkline.new(1..8, height: 3) puts sparkline.render # => ▃▆ # => ▂▅███ # => ▁▄▇█████ # 5 lines high for detailed visualization data = Array.new(100) { rand(100) }sparkline = TTY::Sparkline.new(data, height: 5) puts sparkline.render ``` -------------------------------- ### Managing Buffer Size for Streaming Data with TTY-Sparkline Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Illustrates the use of the `:buffer_size` option in TTY-Sparkline to limit memory usage when streaming data, keeping only the most recent N values. Demonstrates how the buffer size affects the rendered output and that original data remains unmodified. ```ruby require "tty-sparkline" # Keep only last 5 valuessparkline = TTY::Sparkline.new(buffer_size: 5, min: 0)sparkline.push(1, 2, 3, 4, 5, 6, 7, 8) puts sparkline.size # => 5 puts sparkline.render # => ▄▅▆▇█ # Original data is not modified data = [1, 2, 3, 4]sparkline = TTY::Sparkline.new(data, buffer_size: 5, min: 0)sparkline.push(5, 6) puts data.inspect # => [1, 2, 3, 4] puts sparkline.render # => ▃▄▅▆█ ``` -------------------------------- ### Set Min/Max Values for TTY::Sparkline Scaling Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Demonstrates how to explicitly set the minimum and maximum values for scaling a TTY::Sparkline chart using the `:min` and `:max` options. This overrides the default automatic scaling based on the data range, allowing for emphasis or de-emphasis of value differences. ```ruby require "tty-sparkline" # Auto-scaled (default) - 50 looks like zerosparkline = TTY::Sparkline.new([100, 75, 100, 50, 80]) puts sparkline.render # => █▄█▁▅ # With custom min/maxsparkline = TTY::Sparkline.new([100, 75, 100, 50, 80], min: 0, max: 100) puts sparkline.render # => █▄█▁▅ # Emphasize smaller values by setting a higher minsparkline = TTY::Sparkline.new([100, 75, 100, 50, 80], min: 40, max: 100) puts sparkline.render # => █▆█▄▅ ``` -------------------------------- ### Adding Colors to Sparklines with TTY-Sparkline and Pastel Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Demonstrates how to add color to sparkline charts using the render block and the Pastel gem. The block allows conditional coloring of bars based on their values relative to min, max, and average. ```ruby require "tty-sparkline" require "pastel" pastel = Pastel.new data = Array.new(120) { rand(100) } min, max = data.minmax avg = data.sum.fdiv(data.size) sparkline = TTY::Sparkline.new(data, height: 6) chart = sparkline.render do |val, bar, col, row| if val == max pastel.cyan(bar) elsif val == min pastel.magenta(bar) elsif val < avg pastel.red(bar) else pastel.green(bar) end end puts "#{pastel.green("■")} above avg #{pastel.red("■")} below avg " \ "#{pastel.cyan("■")} max #{pastel.magenta("■")} min" puts chart ``` -------------------------------- ### Real-Time Data Streaming with TTY-Sparkline Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Shows how to create a live-updating sparkline chart for real-time data streaming. It combines positioning, width limits, and the append operator (`<<`) to continuously update the chart in the terminal. ```ruby require "tty-sparkline" sparkline = TTY::Sparkline.new(left: 10, top: 10, height: 3, width: 40) print sparkline.cursor.clear_screen loop do # Fetch next value from data source (e.g., API, sensor, log) value = rand(50) sparkline << value print sparkline.render sleep(0.05) end ``` -------------------------------- ### Configure Sparkline Data Range Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md The minimum and maximum values for the sparkline data can be set using the 'min' and 'max' options to control the chart's scaling. ```ruby sparkline = TTY::Sparkline.new(1..8, min: 0, max: 5) ``` -------------------------------- ### Handle Non-Numeric Data in Sparkline Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Defines how sparklines should represent non-numeric data points. Options include `:empty` (default, shows spaces), `:ignore` (skips display), and `:minimum` (shows the smallest bar). This is controlled by the `:non_numeric` keyword argument. ```ruby data = [1, 2, "foo", 4, nil, 6, "", 8] # Default (:empty) sparkline_empty = TTY::Sparkline.new(data) sparkline_empty.render # => "▁▂ ▄ ▆ █" # Ignore non-numeric sparkline_ignore = TTY::Sparkline.new(data, non_numeric: :ignore) sparkline_ignore.render # => "▁▂▄▆█" # Use minimum for non-numeric sparkline_minimum = TTY::Sparkline.new(data, non_numeric: :minimum) sparkline_minimum.render # => "▁▂▁▄▁▆▁█" ``` -------------------------------- ### Handling Non-Numeric Values in TTY-Sparkline Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Explains and demonstrates the `:non_numeric` option in TTY-Sparkline for controlling how non-numeric values (strings, nil, empty strings) are displayed. Options include `:empty` (default), `:ignore`, and `:minimum`. ```ruby require "tty-sparkline" data = [1, 2, "foo", 4, nil, 6, "", 8] # :empty (default) - shows spaces for non-numericsparkline = TTY::Sparkline.new(data) puts sparkline.render # => ▁▂ ▄ ▆ █ # :ignore - skips non-numeric values entirelysparkline = TTY::Sparkline.new(data, non_numeric: :ignore) puts sparkline.render # => ▁▂▄▆█ # :minimum - shows smallest bar for non-numericsparkline = TTY::Sparkline.new(data, non_numeric: :minimum) puts sparkline.render # => ▁▂▁▄▁▆▁█ ``` -------------------------------- ### Configure TTY::Sparkline Width Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Explains how to set the maximum width of a TTY::Sparkline chart using the `:width` option. If the data exceeds the specified width, only the most recent values are displayed, effectively creating a fixed-size window for the data. ```ruby require "tty-sparkline" # Limit to 5 columns (shows last 5 values)sparkline = TTY::Sparkline.new(1..8, width: 5) puts sparkline.render # => ▄▅▆▇█ # Combine height and widthsparkline = TTY::Sparkline.new(1..8, height: 3, width: 5) puts sparkline.render # => ▃▆ # => ▂▅███ # => █████ ``` -------------------------------- ### Colorizing Sparkline Bars with Pastel Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md This snippet demonstrates how to colorize individual bars of a sparkline based on their values using the Pastel gem. It iterates through the sparkline's rendering process, applying different colors (cyan for max, red for min, green for others) to the bars. ```ruby pastel = Pastel.new sparkline.render do |val, bar, col, row| if val == 8 pastel.cyan(bar) elsif val == 1 pastel.red(bar) else pastel.green(bar) end end ``` -------------------------------- ### Configure Sparkline Buffer Size Source: https://github.com/piotrmurach/tty-sparkline/blob/master/README.md Sets the maximum number of data values stored in a sparkline. Defaults to 16K, this can be adjusted using the `:buffer_size` keyword argument to manage memory usage, especially for streaming data. Exceeding the buffer size will truncate older data. ```ruby sparkline = TTY::Sparkline.new(buffer_size: 5, min: 0) sparkline.push(1, 2, 3, 4, 5, 6, 7, 8) sparkline.render # => "▄▅▆▇█" ``` -------------------------------- ### Customizing Sparkline Bar Characters with TTY-Sparkline Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Shows how to replace default sparkline bar characters with custom ones using the `:bars` option. The characters should be provided as an array ordered from smallest to largest. ```ruby require "tty-sparkline" # Custom ASCII barssparkline = TTY::Sparkline.new(1..8, bars: %w[_ - = ^]) puts sparkline.render # => ___--==^ # Custom characters for different visual stylessparkline = TTY::Sparkline.new(1..8, bars: %w[. : | #]) puts sparkline.render ``` -------------------------------- ### Position TTY::Sparkline on Terminal Source: https://context7.com/piotrmurach/tty-sparkline/llms.txt Details how to position a TTY::Sparkline chart on the terminal using the `:top` and `:left` options. These options utilize ANSI escape sequences for cursor positioning, allowing charts to be placed at specific coordinates. ```ruby require "tty-sparkline" # Position 10 lines down from topsparkline = TTY::Sparkline.new(1..8, top: 10) # Position 50 columns from leftsparkline = TTY::Sparkline.new(1..8, left: 50) # Position at specific coordinatessparkline = TTY::Sparkline.new(1..8, top: 10, left: 50) # Dynamic positioning with tty-screen # require "tty-screen" # sparkline = TTY::Sparkline.new(1..8, # top: TTY::Screen.height / 2, # left: TTY::Screen.width / 2 # ) print sparkline.render ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.