#!/usr/bin/env perl
use strict;
use warnings;

use Data::Dumper;
#use lib map { "$ENV{HOME}/sandbox/$_/lib" } qw(Music-BachChoralHarmony); # local author libs
use Music::BachChoralHarmony;

#my $data_file = 'share/jsbach_chorals_harmony.data'; # local author files
#my $key_title = 'share/jsbach_BWV_keys_titles.txt';  # "
my $bach = Music::BachChoralHarmony->new(
#    data_file => $data_file,
#    key_title => $key_title,
);
my $songs = $bach->parse;

my %score = ();

for my $song ( keys %$songs ) {
    for my $event ( @{ $songs->{$song}{events} } ) {
        $score{ $event->{chord} }++;
    }
}
#print Dumper [ map { "$_ => $score{$_}" } sort { $score{$a} <=> $score{$b} } keys %score ];

use Chart::Bars;
my $chart = Chart::Bars->new( 2000, 400 );

$chart->set(
    legend       => 'none',
    title        => 'Chords of All Chorales',
    x_label      => 'Chord',
    y_label      => 'Appears',
    precision    => 0,
    include_zero => 'true',
);

my @sorted = sort { $score{$a} <=> $score{$b} } keys %score;
$chart->add_dataset( @sorted );
$chart->add_dataset( map { $score{$_} } @sorted );

$chart->png("$0.png");
