<commit_before># -*- coding: utf-8 -*-

#
# Run this script to create example images with code snippets.
#

import logging
import inspect
import math
import random
import os
import sys

import cartesius.main as cartesius
import cartesius.elements as elements
import cartesius.charts as charts

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')

examples = []

def test_circles():
    """50 random circles"""
    coordinate_system = cartesius.CoordinateSystem()

    for i in range(50):
        center = (random.randint(-20, 20), random.randint(0, 20))
        coordinate_system.add(
            elements.Circle(
                center,
                radius = random.randint(1, 5),
                transparency_mask = random.randint(0, 255),
                fill_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
                color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))))

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_circles)

def test_piechart_1():
    """ PieChart with default colors """
    coordinate_system = cartesius.CoordinateSystem()

    # list or tuple of two-element tuples (value, label):
    piechart_data = (
        charts.data('abc', 1),
        charts.data('cde', 2),
        charts.data('efg', 4),
        charts.data('ijk', 1),
        charts.data('lmn', 5),
        charts.data('opq', 5),
        charts.data('xyz', 3),
    )
    piechart = charts.PieChart(data=piechart_data, color=(0, 0, 0))
    coordinate_system.add(piechart)

    # No need for axes:
    coordinate_system.add(elements.Axis(horizontal=True, hide=True))
    coordinate_system.add(elements.Axis(vertical=True, hide=True))

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_piechart_1)

def test_piechart_2():
    """ PieChart with custom colors """
    coordinate_system = cartesius.CoordinateSystem()

    piechart_data = (
        charts.data('abc', 1, fill_color=(255, 200, 200)),
        charts.data('cde', 2, fill_color=(200, 255, 200)),
        charts.data('efg', 5, fill_color=(200, 200, 255)),
        charts.data('ijk', 3, fill_color=(255, 255, 255)),
    )
    piechart = charts.PieChart(data=piechart_data, color=(0, 0, 0))

    coordinate_system.add(piechart)

    # No need for axes:
    coordinate_system.add(elements.Axis(horizontal=True, hide=True))
    coordinate_system.add(elements.Axis(vertical=True, hide=True))

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_piechart_2)

def test_barchart_1():
    """ Bar charts with same column width. One with default and the other with custom colors """
    coordinate_system = cartesius.CoordinateSystem()

    barchart_data_1 = (
            charts.data(-1, -.5),
            charts.data(0, .7),
            charts.data(1, 2),
            charts.data(2, 2.7),
            charts.data(3, 4),
            charts.data(4, 3.1),
            charts.data(5, 2.1),
            charts.data(6, 1),
            charts.data(7, -.3)
    )
    barchart_1 = charts.BarChart(vertical=True, data=barchart_data_1, width=0.95)
    coordinate_system.add(barchart_1)

    custom_color = (100, 100, 200)

    barchart_data_2 = (
            charts.data(-1, -.25, fill_color=custom_color),
            charts.data(0, .35, fill_color=custom_color),
            charts.data(1, 1, fill_color=custom_color),
            charts.data(2, 1.35, fill_color=custom_color),
            charts.data(3, 2, fill_color=custom_color),
            charts.data(4, 1.65, fill_color=custom_color),
            charts.data(5, 1, fill_color=custom_color),
            charts.data(6, .5, fill_color=custom_color),
            charts.data(7, -.6, fill_color=custom_color)
    )
    barchart_2 = charts.BarChart(vertical=True, data=barchart_data_2, width=0.75, color=(0, 0, 0))
    coordinate_system.add(barchart_2)

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_barchart_1)

def test_barchart_2():
    """ BarChart with different column widths """
    coordinate_system = cartesius.CoordinateSystem()

    barchart_data = (
            charts.data(-5, -0, -.5),
            charts.data(0, 1, size=.7),
            charts.data(1, 3, size=2),
            charts.data(3, 4, size=4),
            charts.data(4, 5.5, size=3.1),
            charts.data(6, 7, size=2.1),
            charts.data(7, 9, size=1),
    )
    barchart = charts.BarChart(vertical=True, data=barchart_data, color=(0, 0, 0))
    coordinate_system.add(barchart)

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_barchart_2)

def test_barchart_horizontal():
    """ Horizontal bar charts """
    coordinate_system = cartesius.CoordinateSystem()

    barchart_data_1 = (
            charts.data(-1, -.5),
            charts.data(0, .7),
            charts.data(1, 2),
            charts.data(2, 2.7),
            charts.data(3, 4),
            charts.data(4, 3.1),
            charts.data(5, 2.1),
            charts.data(6, 1),
            charts.data(7, -.3)
    )
    barchart_1 = charts.BarChart(horizontal=True, data=barchart_data_1, width=0.95)
    coordinate_system.add(barchart_1)

    custom_color = (100, 100, 200)

    barchart_data_2 = (
            charts.data(-1, -.25, fill_color=custom_color),
            charts.data(0, .35, fill_color=custom_color),
            charts.data(1, 1, fill_color=custom_color),
            charts.data(2, 1.35, fill_color=custom_color),
            charts.data(3, 2, fill_color=custom_color),
            charts.data(4, 1.65, fill_color=custom_color),
            charts.data(5, 1, fill_color=custom_color),
            charts.data(6, .5, fill_color=custom_color),
            charts.data(7, -.6, fill_color=custom_color)
    )
    barchart_2 = charts.BarChart(horizontal=True, data=barchart_data_2, width=0.75, color=(0, 0, 0))
    coordinate_system.add(barchart_2)

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_barchart_horizontal)

def test_barchart_with_generator():
    """ Bar charts with data given as generator function """
    coordinate_system = cartesius.CoordinateSystem()

    # In case you don't want to keep all your data in memory, you can give the data as a
    # generator function that will lazy-load your data. This works for all charts:
    def data_generator():
        for x in range(25):
            yield charts.data(x / 2., 2 * math.sin(x/4.))

    barchart_data_generator = data_generator
    barchart = charts.BarChart(data=barchart_data_generator, vertical=True, width=0.5)
    coordinate_system.add(barchart)

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_barchart_with_generator)

def test_function():
    """ Function math.sin from -4 to 5"""
    coordinate_system = cartesius.CoordinateSystem()
<commit_msg>barchart generator with custom colors<commit_after># -*- coding: utf-8 -*-

#
# Run this script to create example images with code snippets.
#

import logging
import inspect
import math
import random
import os
import sys

import cartesius.main as cartesius
import cartesius.elements as elements
import cartesius.charts as charts

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s')

examples = []

def test_circles():
    """50 random circles"""
    coordinate_system = cartesius.CoordinateSystem()

    for i in range(50):
        center = (random.randint(-20, 20), random.randint(0, 20))
        coordinate_system.add(
            elements.Circle(
                center,
                radius = random.randint(1, 5),
                transparency_mask = random.randint(0, 255),
                fill_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
                color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))))

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_circles)

def test_piechart_1():
    """ PieChart with default colors """
    coordinate_system = cartesius.CoordinateSystem()

    # list or tuple of two-element tuples (value, label):
    piechart_data = (
        charts.data('abc', 1),
        charts.data('cde', 2),
        charts.data('efg', 4),
        charts.data('ijk', 1),
        charts.data('lmn', 5),
        charts.data('opq', 5),
        charts.data('xyz', 3),
    )
    piechart = charts.PieChart(data=piechart_data, color=(0, 0, 0))
    coordinate_system.add(piechart)

    # No need for axes:
    coordinate_system.add(elements.Axis(horizontal=True, hide=True))
    coordinate_system.add(elements.Axis(vertical=True, hide=True))

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_piechart_1)

def test_piechart_2():
    """ PieChart with custom colors """
    coordinate_system = cartesius.CoordinateSystem()

    piechart_data = (
        charts.data('abc', 1, fill_color=(255, 200, 200)),
        charts.data('cde', 2, fill_color=(200, 255, 200)),
        charts.data('efg', 5, fill_color=(200, 200, 255)),
        charts.data('ijk', 3, fill_color=(255, 255, 255)),
    )
    piechart = charts.PieChart(data=piechart_data, color=(0, 0, 0))

    coordinate_system.add(piechart)

    # No need for axes:
    coordinate_system.add(elements.Axis(horizontal=True, hide=True))
    coordinate_system.add(elements.Axis(vertical=True, hide=True))

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_piechart_2)

def test_barchart_1():
    """ Bar charts with same column width. One with default and the other with custom colors """
    coordinate_system = cartesius.CoordinateSystem()

    barchart_data_1 = (
            charts.data(-1, -.5),
            charts.data(0, .7),
            charts.data(1, 2),
            charts.data(2, 2.7),
            charts.data(3, 4),
            charts.data(4, 3.1),
            charts.data(5, 2.1),
            charts.data(6, 1),
            charts.data(7, -.3)
    )
    barchart_1 = charts.BarChart(vertical=True, data=barchart_data_1, width=0.95)
    coordinate_system.add(barchart_1)

    custom_color = (100, 100, 200)

    barchart_data_2 = (
            charts.data(-1, -.25, fill_color=custom_color),
            charts.data(0, .35, fill_color=custom_color),
            charts.data(1, 1, fill_color=custom_color),
            charts.data(2, 1.35, fill_color=custom_color),
            charts.data(3, 2, fill_color=custom_color),
            charts.data(4, 1.65, fill_color=custom_color),
            charts.data(5, 1, fill_color=custom_color),
            charts.data(6, .5, fill_color=custom_color),
            charts.data(7, -.6, fill_color=custom_color)
    )
    barchart_2 = charts.BarChart(vertical=True, data=barchart_data_2, width=0.75, color=(0, 0, 0))
    coordinate_system.add(barchart_2)

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_barchart_1)

def test_barchart_2():
    """ BarChart with different column widths """
    coordinate_system = cartesius.CoordinateSystem()

    barchart_data = (
            charts.data(-5, -0, -.5),
            charts.data(0, 1, size=.7),
            charts.data(1, 3, size=2),
            charts.data(3, 4, size=4),
            charts.data(4, 5.5, size=3.1),
            charts.data(6, 7, size=2.1),
            charts.data(7, 9, size=1),
    )
    barchart = charts.BarChart(vertical=True, data=barchart_data, color=(0, 0, 0))
    coordinate_system.add(barchart)

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_barchart_2)

def test_barchart_horizontal():
    """ Horizontal bar charts """
    coordinate_system = cartesius.CoordinateSystem()

    barchart_data_1 = (
            charts.data(-1, -.5),
            charts.data(0, .7),
            charts.data(1, 2),
            charts.data(2, 2.7),
            charts.data(3, 4),
            charts.data(4, 3.1),
            charts.data(5, 2.1),
            charts.data(6, 1),
            charts.data(7, -.3)
    )
    barchart_1 = charts.BarChart(horizontal=True, data=barchart_data_1, width=0.95)
    coordinate_system.add(barchart_1)

    custom_color = (100, 100, 200)

    barchart_data_2 = (
            charts.data(-1, -.25, fill_color=custom_color),
            charts.data(0, .35, fill_color=custom_color),
            charts.data(1, 1, fill_color=custom_color),
            charts.data(2, 1.35, fill_color=custom_color),
            charts.data(3, 2, fill_color=custom_color),
            charts.data(4, 1.65, fill_color=custom_color),
            charts.data(5, 1, fill_color=custom_color),
            charts.data(6, .5, fill_color=custom_color),
            charts.data(7, -.6, fill_color=custom_color)
    )
    barchart_2 = charts.BarChart(horizontal=True, data=barchart_data_2, width=0.75, color=(0, 0, 0))
    coordinate_system.add(barchart_2)

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_barchart_horizontal)

def test_barchart_with_generator():
    """ Bar charts with data given as generator function """
    import cartesius.colors as colors

    coordinate_system = cartesius.CoordinateSystem()

    bottom_collor = (0, 0, 255)
    top_color = (255, 255, 255)

    # In case you don't want to keep all your data in memory, you can give the data as a
    # generator function that will lazy-load your data. This works for all charts:
    def data_generator():
        for x in range(25):
            key = x / 2.
            value = 2 * math.sin(x/4.)
            color = colors.get_color_between(bottom_collor, top_color, (value + 2) / 4.)
            yield charts.data(key, value, fill_color=color)

    barchart_data_generator = data_generator
    barchart = charts.BarChart(data=barchart_data_generator, vertical=True, width=0.5)
    coordinate_system.add(barchart)

    return coordinate_system.draw(400, 250), coordinate_system.draw(400, 250, antialiasing=True),

examples.append(test_barchart_with_generator)

def test_function():
    """ Function math.sin from -4 to 5"""
    coordinate_system = cartesius.CoordinateSystem()

{'fraction_non_alphanumeric': 0.1320793293804948, 'fraction_numerical': 0.05125059633340148, 'mean_word_length': 2.9994548923412374, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4568549', 'n_tokens_mistral': 5336, 'n_tokens_neox': 4781, 'n_words': 991}